]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Library/PciHostBridgeUtilityLib/PciHostBridgeUtilityLib.c
OvmfPkg/PciHostBridgeUtilityLib: Extend parameters of InitRootBridge()
[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 <Library/BaseMemoryLib.h>
15 #include <Library/DebugLib.h>
16 #include <Library/DevicePathLib.h>
17 #include <Library/MemoryAllocationLib.h>
18 #include <Library/PciHostBridgeUtilityLib.h>
19
20
21 #pragma pack(1)
22 typedef struct {
23 ACPI_HID_DEVICE_PATH AcpiDevicePath;
24 EFI_DEVICE_PATH_PROTOCOL EndDevicePath;
25 } OVMF_PCI_ROOT_BRIDGE_DEVICE_PATH;
26 #pragma pack ()
27
28
29 GLOBAL_REMOVE_IF_UNREFERENCED
30 CHAR16 *mPciHostBridgeUtilityLibAcpiAddressSpaceTypeStr[] = {
31 L"Mem", L"I/O", L"Bus"
32 };
33
34
35 STATIC
36 CONST
37 OVMF_PCI_ROOT_BRIDGE_DEVICE_PATH mRootBridgeDevicePathTemplate = {
38 {
39 {
40 ACPI_DEVICE_PATH,
41 ACPI_DP,
42 {
43 (UINT8) (sizeof(ACPI_HID_DEVICE_PATH)),
44 (UINT8) ((sizeof(ACPI_HID_DEVICE_PATH)) >> 8)
45 }
46 },
47 EISA_PNP_ID(0x0A03), // HID
48 0 // UID
49 },
50
51 {
52 END_DEVICE_PATH_TYPE,
53 END_ENTIRE_DEVICE_PATH_SUBTYPE,
54 {
55 END_DEVICE_PATH_LENGTH,
56 0
57 }
58 }
59 };
60
61
62 /**
63 Utility function to initialize a PCI_ROOT_BRIDGE structure.
64
65 @param[in] Supports Supported attributes.
66
67 @param[in] Attributes Initial attributes.
68
69 @param[in] AllocAttributes Allocation attributes.
70
71 @param[in] DmaAbove4G DMA above 4GB memory.
72
73 @param[in] NoExtendedConfigSpace No Extended Config Space.
74
75 @param[in] RootBusNumber The bus number to store in RootBus.
76
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
80 root bus.
81
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
86 buses.
87
88 @param[in] Io IO aperture.
89
90 @param[in] Mem MMIO aperture.
91
92 @param[in] MemAbove4G MMIO aperture above 4G.
93
94 @param[in] PMem Prefetchable MMIO aperture.
95
96 @param[in] PMemAbove4G Prefetchable MMIO aperture above 4G.
97
98 @param[out] RootBus The PCI_ROOT_BRIDGE structure (allocated
99 by the caller) that should be filled in by
100 this function.
101
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.
106
107 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
108 **/
109 EFI_STATUS
110 EFIAPI
111 PciHostBridgeUtilityInitRootBridge (
112 IN UINT64 Supports,
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
125 )
126 {
127 OVMF_PCI_ROOT_BRIDGE_DEVICE_PATH *DevicePath;
128
129 //
130 // Be safe if other fields are added to PCI_ROOT_BRIDGE later.
131 //
132 ZeroMem (RootBus, sizeof *RootBus);
133
134 RootBus->Segment = 0;
135
136 RootBus->Supports = Supports;
137 RootBus->Attributes = Attributes;
138
139 RootBus->DmaAbove4G = DmaAbove4G;
140
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));
149
150 RootBus->NoExtendedConfigSpace = NoExtendedConfigSpace;
151
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;
157 }
158 DevicePath->AcpiDevicePath.UID = RootBusNumber;
159 RootBus->DevicePath = (EFI_DEVICE_PATH_PROTOCOL *)DevicePath;
160
161 DEBUG ((DEBUG_INFO,
162 "%a: populated root bus %d, with room for %d subordinate bus(es)\n",
163 __FUNCTION__, RootBusNumber, MaxSubBusNumber - RootBusNumber));
164 return EFI_SUCCESS;
165 }
166
167
168 /**
169 Utility function to uninitialize a PCI_ROOT_BRIDGE structure set up with
170 PciHostBridgeUtilityInitRootBridge().
171
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
175 RootBus.
176 **/
177 VOID
178 EFIAPI
179 PciHostBridgeUtilityUninitRootBridge (
180 IN PCI_ROOT_BRIDGE *RootBus
181 )
182 {
183 FreePool (RootBus->DevicePath);
184 }
185
186
187 /**
188 Utility function to inform the platform that the resource conflict happens.
189
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
197 description in
198 EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
199 .SubmitResources().
200 **/
201 VOID
202 EFIAPI
203 PciHostBridgeUtilityResourceConflict (
204 IN VOID *Configuration
205 )
206 {
207 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Descriptor;
208 UINTN RootBridgeIndex;
209 DEBUG ((DEBUG_ERROR, "PciHostBridge: Resource conflict happens!\n"));
210
211 RootBridgeIndex = 0;
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)
218 );
219 DEBUG ((DEBUG_ERROR, " %s: Length/Alignment = 0x%lx / 0x%lx\n",
220 mPciHostBridgeUtilityLibAcpiAddressSpaceTypeStr[Descriptor->ResType],
221 Descriptor->AddrLen, Descriptor->AddrRangeMax
222 ));
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""
229 ));
230 }
231 }
232 //
233 // Skip the END descriptor for root bridge
234 //
235 ASSERT (Descriptor->Desc == ACPI_END_TAG_DESCRIPTOR);
236 Descriptor = (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *)(
237 (EFI_ACPI_END_TAG_DESCRIPTOR *)Descriptor + 1
238 );
239 }
240 }
241