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 <IndustryStandard/Q35MchIch9.h>
15 #include <Library/BaseMemoryLib.h>
16 #include <Library/DebugLib.h>
17 #include <Library/DevicePathLib.h>
18 #include <Library/MemoryAllocationLib.h>
19 #include <Library/PcdLib.h>
20 #include <Library/PciHostBridgeUtilityLib.h>
25 ACPI_HID_DEVICE_PATH AcpiDevicePath
;
26 EFI_DEVICE_PATH_PROTOCOL EndDevicePath
;
27 } OVMF_PCI_ROOT_BRIDGE_DEVICE_PATH
;
31 GLOBAL_REMOVE_IF_UNREFERENCED
32 CHAR16
*mPciHostBridgeUtilityLibAcpiAddressSpaceTypeStr
[] = {
33 L
"Mem", L
"I/O", L
"Bus"
39 OVMF_PCI_ROOT_BRIDGE_DEVICE_PATH mRootBridgeDevicePathTemplate
= {
45 (UINT8
) (sizeof(ACPI_HID_DEVICE_PATH
)),
46 (UINT8
) ((sizeof(ACPI_HID_DEVICE_PATH
)) >> 8)
49 EISA_PNP_ID(0x0A03), // HID
55 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
57 END_DEVICE_PATH_LENGTH
,
65 Utility function to initialize a PCI_ROOT_BRIDGE structure.
67 @param[in] Supports Supported attributes.
69 @param[in] Attributes Initial attributes.
71 @param[in] AllocAttributes Allocation attributes.
73 @param[in] RootBusNumber The bus number to store in RootBus.
75 @param[in] MaxSubBusNumber The inclusive maximum bus number that can be
76 assigned to any subordinate bus found behind any
77 PCI bridge hanging off this root bus.
79 The caller is repsonsible for ensuring that
80 RootBusNumber <= MaxSubBusNumber. If
81 RootBusNumber equals MaxSubBusNumber, then the
82 root bus has no room for subordinate buses.
84 @param[in] Io IO aperture.
86 @param[in] Mem MMIO aperture.
88 @param[in] MemAbove4G MMIO aperture above 4G.
90 @param[in] PMem Prefetchable MMIO aperture.
92 @param[in] PMemAbove4G Prefetchable MMIO aperture above 4G.
94 @param[out] RootBus The PCI_ROOT_BRIDGE structure (allocated by the
95 caller) that should be filled in by this
98 @retval EFI_SUCCESS Initialization successful. A device path
99 consisting of an ACPI device path node, with
100 UID = RootBusNumber, has been allocated and
103 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
107 PciHostBridgeUtilityInitRootBridge (
109 IN UINT64 Attributes
,
110 IN UINT64 AllocAttributes
,
111 IN UINT8 RootBusNumber
,
112 IN UINT8 MaxSubBusNumber
,
113 IN PCI_ROOT_BRIDGE_APERTURE
*Io
,
114 IN PCI_ROOT_BRIDGE_APERTURE
*Mem
,
115 IN PCI_ROOT_BRIDGE_APERTURE
*MemAbove4G
,
116 IN PCI_ROOT_BRIDGE_APERTURE
*PMem
,
117 IN PCI_ROOT_BRIDGE_APERTURE
*PMemAbove4G
,
118 OUT PCI_ROOT_BRIDGE
*RootBus
121 OVMF_PCI_ROOT_BRIDGE_DEVICE_PATH
*DevicePath
;
124 // Be safe if other fields are added to PCI_ROOT_BRIDGE later.
126 ZeroMem (RootBus
, sizeof *RootBus
);
128 RootBus
->Segment
= 0;
130 RootBus
->Supports
= Supports
;
131 RootBus
->Attributes
= Attributes
;
133 RootBus
->DmaAbove4G
= FALSE
;
135 RootBus
->AllocationAttributes
= AllocAttributes
;
136 RootBus
->Bus
.Base
= RootBusNumber
;
137 RootBus
->Bus
.Limit
= MaxSubBusNumber
;
138 CopyMem (&RootBus
->Io
, Io
, sizeof (*Io
));
139 CopyMem (&RootBus
->Mem
, Mem
, sizeof (*Mem
));
140 CopyMem (&RootBus
->MemAbove4G
, MemAbove4G
, sizeof (*MemAbove4G
));
141 CopyMem (&RootBus
->PMem
, PMem
, sizeof (*PMem
));
142 CopyMem (&RootBus
->PMemAbove4G
, PMemAbove4G
, sizeof (*PMemAbove4G
));
144 RootBus
->NoExtendedConfigSpace
= (PcdGet16 (PcdOvmfHostBridgePciDevId
) !=
145 INTEL_Q35_MCH_DEVICE_ID
);
147 DevicePath
= AllocateCopyPool (sizeof mRootBridgeDevicePathTemplate
,
148 &mRootBridgeDevicePathTemplate
);
149 if (DevicePath
== NULL
) {
150 DEBUG ((DEBUG_ERROR
, "%a: %r\n", __FUNCTION__
, EFI_OUT_OF_RESOURCES
));
151 return EFI_OUT_OF_RESOURCES
;
153 DevicePath
->AcpiDevicePath
.UID
= RootBusNumber
;
154 RootBus
->DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*)DevicePath
;
157 "%a: populated root bus %d, with room for %d subordinate bus(es)\n",
158 __FUNCTION__
, RootBusNumber
, MaxSubBusNumber
- RootBusNumber
));
164 Utility function to uninitialize a PCI_ROOT_BRIDGE structure set up with
165 PciHostBridgeUtilityInitRootBridge().
167 @param[in] RootBus The PCI_ROOT_BRIDGE structure, allocated by the caller and
168 initialized with PciHostBridgeUtilityInitRootBridge(),
169 that should be uninitialized. This function doesn't free
174 PciHostBridgeUtilityUninitRootBridge (
175 IN PCI_ROOT_BRIDGE
*RootBus
178 FreePool (RootBus
->DevicePath
);
183 Utility function to inform the platform that the resource conflict happens.
185 @param[in] Configuration Pointer to PCI I/O and PCI memory resource
186 descriptors. The Configuration contains the
187 resources for all the root bridges. The resource
188 for each root bridge is terminated with END
189 descriptor and an additional END is appended
190 indicating the end of the entire resources. The
191 resource descriptor field values follow the
193 EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
198 PciHostBridgeUtilityResourceConflict (
199 IN VOID
*Configuration
202 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Descriptor
;
203 UINTN RootBridgeIndex
;
204 DEBUG ((DEBUG_ERROR
, "PciHostBridge: Resource conflict happens!\n"));
207 Descriptor
= (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*) Configuration
;
208 while (Descriptor
->Desc
== ACPI_ADDRESS_SPACE_DESCRIPTOR
) {
209 DEBUG ((DEBUG_ERROR
, "RootBridge[%d]:\n", RootBridgeIndex
++));
210 for (; Descriptor
->Desc
== ACPI_ADDRESS_SPACE_DESCRIPTOR
; Descriptor
++) {
211 ASSERT (Descriptor
->ResType
<
212 ARRAY_SIZE (mPciHostBridgeUtilityLibAcpiAddressSpaceTypeStr
)
214 DEBUG ((DEBUG_ERROR
, " %s: Length/Alignment = 0x%lx / 0x%lx\n",
215 mPciHostBridgeUtilityLibAcpiAddressSpaceTypeStr
[Descriptor
->ResType
],
216 Descriptor
->AddrLen
, Descriptor
->AddrRangeMax
218 if (Descriptor
->ResType
== ACPI_ADDRESS_SPACE_TYPE_MEM
) {
219 DEBUG ((DEBUG_ERROR
, " Granularity/SpecificFlag = %ld / %02x%s\n",
220 Descriptor
->AddrSpaceGranularity
, Descriptor
->SpecificFlag
,
221 ((Descriptor
->SpecificFlag
&
222 EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE
223 ) != 0) ? L
" (Prefetchable)" : L
""
228 // Skip the END descriptor for root bridge
230 ASSERT (Descriptor
->Desc
== ACPI_END_TAG_DESCRIPTOR
);
231 Descriptor
= (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*)(
232 (EFI_ACPI_END_TAG_DESCRIPTOR
*)Descriptor
+ 1