2 Provide common utility functions to PciHostBridgeLib instances in
3 ArmVirtPkg and OvmfPkg.
5 Copyright (C) 2016, Red Hat, Inc.
6 Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
7 Copyright (c) 2020, Huawei Corporation. All rights reserved.<BR>
9 SPDX-License-Identifier: BSD-2-Clause-Patent
13 #include <IndustryStandard/Acpi10.h>
14 #include <Library/BaseMemoryLib.h>
15 #include <Library/DebugLib.h>
16 #include <Library/DevicePathLib.h>
17 #include <Library/MemoryAllocationLib.h>
18 #include <Library/PciHostBridgeUtilityLib.h>
23 ACPI_HID_DEVICE_PATH AcpiDevicePath
;
24 EFI_DEVICE_PATH_PROTOCOL EndDevicePath
;
25 } OVMF_PCI_ROOT_BRIDGE_DEVICE_PATH
;
29 GLOBAL_REMOVE_IF_UNREFERENCED
30 CHAR16
*mPciHostBridgeUtilityLibAcpiAddressSpaceTypeStr
[] = {
31 L
"Mem", L
"I/O", L
"Bus"
37 OVMF_PCI_ROOT_BRIDGE_DEVICE_PATH mRootBridgeDevicePathTemplate
= {
43 (UINT8
) (sizeof(ACPI_HID_DEVICE_PATH
)),
44 (UINT8
) ((sizeof(ACPI_HID_DEVICE_PATH
)) >> 8)
47 EISA_PNP_ID(0x0A03), // HID
53 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
55 END_DEVICE_PATH_LENGTH
,
63 Utility function to initialize a PCI_ROOT_BRIDGE structure.
65 @param[in] Supports Supported attributes.
67 @param[in] Attributes Initial attributes.
69 @param[in] AllocAttributes Allocation attributes.
71 @param[in] DmaAbove4G DMA above 4GB memory.
73 @param[in] NoExtendedConfigSpace No Extended Config Space.
75 @param[in] RootBusNumber The bus number to store in RootBus.
77 @param[in] MaxSubBusNumber The inclusive maximum bus number that can
78 be assigned to any subordinate bus found
79 behind any PCI bridge hanging off this
82 The caller is repsonsible for ensuring
83 that RootBusNumber <= MaxSubBusNumber. If
84 RootBusNumber equals MaxSubBusNumber, then
85 the root bus has no room for subordinate
88 @param[in] Io IO aperture.
90 @param[in] Mem MMIO aperture.
92 @param[in] MemAbove4G MMIO aperture above 4G.
94 @param[in] PMem Prefetchable MMIO aperture.
96 @param[in] PMemAbove4G Prefetchable MMIO aperture above 4G.
98 @param[out] RootBus The PCI_ROOT_BRIDGE structure (allocated
99 by the caller) that should be filled in by
102 @retval EFI_SUCCESS Initialization successful. A device path
103 consisting of an ACPI device path node,
104 with UID = RootBusNumber, has been
105 allocated and linked into RootBus.
107 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
111 PciHostBridgeUtilityInitRootBridge (
113 IN UINT64 Attributes
,
114 IN UINT64 AllocAttributes
,
115 IN BOOLEAN DmaAbove4G
,
116 IN BOOLEAN NoExtendedConfigSpace
,
117 IN UINT8 RootBusNumber
,
118 IN UINT8 MaxSubBusNumber
,
119 IN PCI_ROOT_BRIDGE_APERTURE
*Io
,
120 IN PCI_ROOT_BRIDGE_APERTURE
*Mem
,
121 IN PCI_ROOT_BRIDGE_APERTURE
*MemAbove4G
,
122 IN PCI_ROOT_BRIDGE_APERTURE
*PMem
,
123 IN PCI_ROOT_BRIDGE_APERTURE
*PMemAbove4G
,
124 OUT PCI_ROOT_BRIDGE
*RootBus
127 OVMF_PCI_ROOT_BRIDGE_DEVICE_PATH
*DevicePath
;
130 // Be safe if other fields are added to PCI_ROOT_BRIDGE later.
132 ZeroMem (RootBus
, sizeof *RootBus
);
134 RootBus
->Segment
= 0;
136 RootBus
->Supports
= Supports
;
137 RootBus
->Attributes
= Attributes
;
139 RootBus
->DmaAbove4G
= DmaAbove4G
;
141 RootBus
->AllocationAttributes
= AllocAttributes
;
142 RootBus
->Bus
.Base
= RootBusNumber
;
143 RootBus
->Bus
.Limit
= MaxSubBusNumber
;
144 CopyMem (&RootBus
->Io
, Io
, sizeof (*Io
));
145 CopyMem (&RootBus
->Mem
, Mem
, sizeof (*Mem
));
146 CopyMem (&RootBus
->MemAbove4G
, MemAbove4G
, sizeof (*MemAbove4G
));
147 CopyMem (&RootBus
->PMem
, PMem
, sizeof (*PMem
));
148 CopyMem (&RootBus
->PMemAbove4G
, PMemAbove4G
, sizeof (*PMemAbove4G
));
150 RootBus
->NoExtendedConfigSpace
= NoExtendedConfigSpace
;
152 DevicePath
= AllocateCopyPool (sizeof mRootBridgeDevicePathTemplate
,
153 &mRootBridgeDevicePathTemplate
);
154 if (DevicePath
== NULL
) {
155 DEBUG ((DEBUG_ERROR
, "%a: %r\n", __FUNCTION__
, EFI_OUT_OF_RESOURCES
));
156 return EFI_OUT_OF_RESOURCES
;
158 DevicePath
->AcpiDevicePath
.UID
= RootBusNumber
;
159 RootBus
->DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*)DevicePath
;
162 "%a: populated root bus %d, with room for %d subordinate bus(es)\n",
163 __FUNCTION__
, RootBusNumber
, MaxSubBusNumber
- RootBusNumber
));
169 Utility function to uninitialize a PCI_ROOT_BRIDGE structure set up with
170 PciHostBridgeUtilityInitRootBridge().
172 @param[in] RootBus The PCI_ROOT_BRIDGE structure, allocated by the caller and
173 initialized with PciHostBridgeUtilityInitRootBridge(),
174 that should be uninitialized. This function doesn't free
179 PciHostBridgeUtilityUninitRootBridge (
180 IN PCI_ROOT_BRIDGE
*RootBus
183 FreePool (RootBus
->DevicePath
);
188 Utility function to inform the platform that the resource conflict happens.
190 @param[in] Configuration Pointer to PCI I/O and PCI memory resource
191 descriptors. The Configuration contains the
192 resources for all the root bridges. The resource
193 for each root bridge is terminated with END
194 descriptor and an additional END is appended
195 indicating the end of the entire resources. The
196 resource descriptor field values follow the
198 EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
203 PciHostBridgeUtilityResourceConflict (
204 IN VOID
*Configuration
207 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Descriptor
;
208 UINTN RootBridgeIndex
;
209 DEBUG ((DEBUG_ERROR
, "PciHostBridge: Resource conflict happens!\n"));
212 Descriptor
= (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*) Configuration
;
213 while (Descriptor
->Desc
== ACPI_ADDRESS_SPACE_DESCRIPTOR
) {
214 DEBUG ((DEBUG_ERROR
, "RootBridge[%d]:\n", RootBridgeIndex
++));
215 for (; Descriptor
->Desc
== ACPI_ADDRESS_SPACE_DESCRIPTOR
; Descriptor
++) {
216 ASSERT (Descriptor
->ResType
<
217 ARRAY_SIZE (mPciHostBridgeUtilityLibAcpiAddressSpaceTypeStr
)
219 DEBUG ((DEBUG_ERROR
, " %s: Length/Alignment = 0x%lx / 0x%lx\n",
220 mPciHostBridgeUtilityLibAcpiAddressSpaceTypeStr
[Descriptor
->ResType
],
221 Descriptor
->AddrLen
, Descriptor
->AddrRangeMax
223 if (Descriptor
->ResType
== ACPI_ADDRESS_SPACE_TYPE_MEM
) {
224 DEBUG ((DEBUG_ERROR
, " Granularity/SpecificFlag = %ld / %02x%s\n",
225 Descriptor
->AddrSpaceGranularity
, Descriptor
->SpecificFlag
,
226 ((Descriptor
->SpecificFlag
&
227 EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE
228 ) != 0) ? L
" (Prefetchable)" : L
""
233 // Skip the END descriptor for root bridge
235 ASSERT (Descriptor
->Desc
== ACPI_END_TAG_DESCRIPTOR
);
236 Descriptor
= (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*)(
237 (EFI_ACPI_END_TAG_DESCRIPTOR
*)Descriptor
+ 1