]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Library/PciHostBridgeUtilityLib/PciHostBridgeUtilityLib.c
OvmfPkg/PciHostBridgeLib: Extract InitRootBridge() / UninitRootBridge()
[mirror_edk2.git] / OvmfPkg / Library / PciHostBridgeUtilityLib / PciHostBridgeUtilityLib.c
1 /** @file
2 Provide common utility functions to PciHostBridgeLib instances in
3 ArmVirtPkg and OvmfPkg.
4
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>
8
9 SPDX-License-Identifier: BSD-2-Clause-Patent
10
11 **/
12
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>
21
22
23 #pragma pack(1)
24 typedef struct {
25 ACPI_HID_DEVICE_PATH AcpiDevicePath;
26 EFI_DEVICE_PATH_PROTOCOL EndDevicePath;
27 } OVMF_PCI_ROOT_BRIDGE_DEVICE_PATH;
28 #pragma pack ()
29
30
31 GLOBAL_REMOVE_IF_UNREFERENCED
32 CHAR16 *mPciHostBridgeUtilityLibAcpiAddressSpaceTypeStr[] = {
33 L"Mem", L"I/O", L"Bus"
34 };
35
36
37 STATIC
38 CONST
39 OVMF_PCI_ROOT_BRIDGE_DEVICE_PATH mRootBridgeDevicePathTemplate = {
40 {
41 {
42 ACPI_DEVICE_PATH,
43 ACPI_DP,
44 {
45 (UINT8) (sizeof(ACPI_HID_DEVICE_PATH)),
46 (UINT8) ((sizeof(ACPI_HID_DEVICE_PATH)) >> 8)
47 }
48 },
49 EISA_PNP_ID(0x0A03), // HID
50 0 // UID
51 },
52
53 {
54 END_DEVICE_PATH_TYPE,
55 END_ENTIRE_DEVICE_PATH_SUBTYPE,
56 {
57 END_DEVICE_PATH_LENGTH,
58 0
59 }
60 }
61 };
62
63
64 /**
65 Utility function to initialize a PCI_ROOT_BRIDGE structure.
66
67 @param[in] Supports Supported attributes.
68
69 @param[in] Attributes Initial attributes.
70
71 @param[in] AllocAttributes Allocation attributes.
72
73 @param[in] RootBusNumber The bus number to store in RootBus.
74
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.
78
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.
83
84 @param[in] Io IO aperture.
85
86 @param[in] Mem MMIO aperture.
87
88 @param[in] MemAbove4G MMIO aperture above 4G.
89
90 @param[in] PMem Prefetchable MMIO aperture.
91
92 @param[in] PMemAbove4G Prefetchable MMIO aperture above 4G.
93
94 @param[out] RootBus The PCI_ROOT_BRIDGE structure (allocated by the
95 caller) that should be filled in by this
96 function.
97
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
101 linked into RootBus.
102
103 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
104 **/
105 EFI_STATUS
106 EFIAPI
107 PciHostBridgeUtilityInitRootBridge (
108 IN UINT64 Supports,
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
119 )
120 {
121 OVMF_PCI_ROOT_BRIDGE_DEVICE_PATH *DevicePath;
122
123 //
124 // Be safe if other fields are added to PCI_ROOT_BRIDGE later.
125 //
126 ZeroMem (RootBus, sizeof *RootBus);
127
128 RootBus->Segment = 0;
129
130 RootBus->Supports = Supports;
131 RootBus->Attributes = Attributes;
132
133 RootBus->DmaAbove4G = FALSE;
134
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));
143
144 RootBus->NoExtendedConfigSpace = (PcdGet16 (PcdOvmfHostBridgePciDevId) !=
145 INTEL_Q35_MCH_DEVICE_ID);
146
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;
152 }
153 DevicePath->AcpiDevicePath.UID = RootBusNumber;
154 RootBus->DevicePath = (EFI_DEVICE_PATH_PROTOCOL *)DevicePath;
155
156 DEBUG ((DEBUG_INFO,
157 "%a: populated root bus %d, with room for %d subordinate bus(es)\n",
158 __FUNCTION__, RootBusNumber, MaxSubBusNumber - RootBusNumber));
159 return EFI_SUCCESS;
160 }
161
162
163 /**
164 Utility function to uninitialize a PCI_ROOT_BRIDGE structure set up with
165 PciHostBridgeUtilityInitRootBridge().
166
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
170 RootBus.
171 **/
172 VOID
173 EFIAPI
174 PciHostBridgeUtilityUninitRootBridge (
175 IN PCI_ROOT_BRIDGE *RootBus
176 )
177 {
178 FreePool (RootBus->DevicePath);
179 }
180
181
182 /**
183 Utility function to inform the platform that the resource conflict happens.
184
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
192 description in
193 EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
194 .SubmitResources().
195 **/
196 VOID
197 EFIAPI
198 PciHostBridgeUtilityResourceConflict (
199 IN VOID *Configuration
200 )
201 {
202 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Descriptor;
203 UINTN RootBridgeIndex;
204 DEBUG ((DEBUG_ERROR, "PciHostBridge: Resource conflict happens!\n"));
205
206 RootBridgeIndex = 0;
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)
213 );
214 DEBUG ((DEBUG_ERROR, " %s: Length/Alignment = 0x%lx / 0x%lx\n",
215 mPciHostBridgeUtilityLibAcpiAddressSpaceTypeStr[Descriptor->ResType],
216 Descriptor->AddrLen, Descriptor->AddrRangeMax
217 ));
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""
224 ));
225 }
226 }
227 //
228 // Skip the END descriptor for root bridge
229 //
230 ASSERT (Descriptor->Desc == ACPI_END_TAG_DESCRIPTOR);
231 Descriptor = (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *)(
232 (EFI_ACPI_END_TAG_DESCRIPTOR *)Descriptor + 1
233 );
234 }
235 }
236