]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Library/PciHostBridgeLib/PciHostBridgeLib.c
OvmfPkg: PciHostBridgeLib: set RootBus->Segment
[mirror_edk2.git] / OvmfPkg / Library / PciHostBridgeLib / PciHostBridgeLib.c
1 /** @file
2 OVMF's instance of the PCI Host Bridge Library.
3
4 Copyright (C) 2016, Red Hat, Inc.
5 Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
6
7 This program and the accompanying materials are licensed and made available
8 under the terms and conditions of the BSD License which accompanies this
9 distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php.
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
13 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16 #include <PiDxe.h>
17
18 #include <IndustryStandard/Pci.h>
19
20 #include <Library/BaseMemoryLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/MemoryAllocationLib.h>
23 #include <Library/PciHostBridgeLib.h>
24 #include <Library/PciLib.h>
25 #include <Library/QemuFwCfgLib.h>
26
27
28 GLOBAL_REMOVE_IF_UNREFERENCED
29 CHAR16 *mPciHostBridgeLibAcpiAddressSpaceTypeStr[] = {
30 L"Mem", L"I/O", L"Bus"
31 };
32
33
34 /**
35 Initialize a PCI_ROOT_BRIDGE structure.
36
37 param[in] RootBusNumber The bus number to store in RootBus.
38
39 param[in] MaxSubBusNumber The inclusive maximum bus number that can be
40 assigned to any subordinate bus found behind any
41 PCI bridge hanging off this root bus.
42
43 The caller is repsonsible for ensuring that
44 RootBusNumber <= MaxSubBusNumber. If
45 RootBusNumber equals MaxSubBusNumber, then the
46 root bus has no room for subordinate buses.
47
48 param[out] RootBus The PCI_ROOT_BRIDGE structure (allocated by the
49 caller) that should be filled in by this
50 function.
51
52 @retval EFI_SUCCESS Initialization successful. A device path
53 consisting of an ACPI device path node, with
54 UID = RootBusNumber, has been allocated and
55 linked into RootBus.
56
57 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
58 **/
59 STATIC
60 EFI_STATUS
61 InitRootBridge (
62 IN UINT8 RootBusNumber,
63 IN UINT8 MaxSubBusNumber,
64 OUT PCI_ROOT_BRIDGE *RootBus
65 )
66 {
67 //
68 // Be safe if other fields are added to PCI_ROOT_BRIDGE later.
69 //
70 ZeroMem (RootBus, sizeof *RootBus);
71
72 RootBus->Segment = 0;
73
74 return EFI_OUT_OF_RESOURCES;
75 }
76
77
78 /**
79 Uninitialize a PCI_ROOT_BRIDGE structure set up with InitRootBridge().
80
81 param[in] RootBus The PCI_ROOT_BRIDGE structure, allocated by the caller and
82 initialized with InitRootBridge(), that should be
83 uninitialized. This function doesn't free RootBus.
84 **/
85 STATIC
86 VOID
87 UninitRootBridge (
88 IN PCI_ROOT_BRIDGE *RootBus
89 )
90 {
91 }
92
93
94 /**
95 Return all the root bridge instances in an array.
96
97 @param Count Return the count of root bridge instances.
98
99 @return All the root bridge instances in an array.
100 The array should be passed into PciHostBridgeFreeRootBridges()
101 when it's not used.
102 **/
103 PCI_ROOT_BRIDGE *
104 EFIAPI
105 PciHostBridgeGetRootBridges (
106 UINTN *Count
107 )
108 {
109 EFI_STATUS Status;
110 FIRMWARE_CONFIG_ITEM FwCfgItem;
111 UINTN FwCfgSize;
112 UINT64 ExtraRootBridges;
113 PCI_ROOT_BRIDGE *Bridges;
114 UINTN Initialized;
115 UINTN LastRootBridgeNumber;
116 UINTN RootBridgeNumber;
117
118 *Count = 0;
119
120 //
121 // QEMU provides the number of extra root buses, shortening the exhaustive
122 // search below. If there is no hint, the feature is missing.
123 //
124 Status = QemuFwCfgFindFile ("etc/extra-pci-roots", &FwCfgItem, &FwCfgSize);
125 if (EFI_ERROR (Status) || FwCfgSize != sizeof ExtraRootBridges) {
126 ExtraRootBridges = 0;
127 } else {
128 QemuFwCfgSelectItem (FwCfgItem);
129 QemuFwCfgReadBytes (FwCfgSize, &ExtraRootBridges);
130
131 if (ExtraRootBridges > PCI_MAX_BUS) {
132 DEBUG ((EFI_D_ERROR, "%a: invalid count of extra root buses (%Lu) "
133 "reported by QEMU\n", __FUNCTION__, ExtraRootBridges));
134 return NULL;
135 }
136 DEBUG ((EFI_D_INFO, "%a: %Lu extra root buses reported by QEMU\n",
137 __FUNCTION__, ExtraRootBridges));
138 }
139
140 //
141 // Allocate the "main" root bridge, and any extra root bridges.
142 //
143 Bridges = AllocatePool ((1 + (UINTN)ExtraRootBridges) * sizeof *Bridges);
144 if (Bridges == NULL) {
145 DEBUG ((EFI_D_ERROR, "%a: %r\n", __FUNCTION__, EFI_OUT_OF_RESOURCES));
146 return NULL;
147 }
148 Initialized = 0;
149
150 //
151 // The "main" root bus is always there.
152 //
153 LastRootBridgeNumber = 0;
154
155 //
156 // Scan all other root buses. If function 0 of any device on a bus returns a
157 // VendorId register value different from all-bits-one, then that bus is
158 // alive.
159 //
160 for (RootBridgeNumber = 1;
161 RootBridgeNumber <= PCI_MAX_BUS && Initialized < ExtraRootBridges;
162 ++RootBridgeNumber) {
163 UINTN Device;
164
165 for (Device = 0; Device <= PCI_MAX_DEVICE; ++Device) {
166 if (PciRead16 (PCI_LIB_ADDRESS (RootBridgeNumber, Device, 0,
167 PCI_VENDOR_ID_OFFSET)) != MAX_UINT16) {
168 break;
169 }
170 }
171 if (Device <= PCI_MAX_DEVICE) {
172 //
173 // Found the next root bus. We can now install the *previous* one,
174 // because now we know how big a bus number range *that* one has, for any
175 // subordinate buses that might exist behind PCI bridges hanging off it.
176 //
177 Status = InitRootBridge ((UINT8)LastRootBridgeNumber,
178 (UINT8)(RootBridgeNumber - 1), &Bridges[Initialized]);
179 if (EFI_ERROR (Status)) {
180 goto FreeBridges;
181 }
182 ++Initialized;
183 LastRootBridgeNumber = RootBridgeNumber;
184 }
185 }
186
187 //
188 // Install the last root bus (which might be the only, ie. main, root bus, if
189 // we've found no extra root buses).
190 //
191 Status = InitRootBridge ((UINT8)LastRootBridgeNumber, PCI_MAX_BUS,
192 &Bridges[Initialized]);
193 if (EFI_ERROR (Status)) {
194 goto FreeBridges;
195 }
196 ++Initialized;
197
198 *Count = Initialized;
199 return Bridges;
200
201 FreeBridges:
202 while (Initialized > 0) {
203 --Initialized;
204 UninitRootBridge (&Bridges[Initialized]);
205 }
206
207 FreePool (Bridges);
208 return NULL;
209 }
210
211
212 /**
213 Free the root bridge instances array returned from
214 PciHostBridgeGetRootBridges().
215
216 @param The root bridge instances array.
217 @param The count of the array.
218 **/
219 VOID
220 EFIAPI
221 PciHostBridgeFreeRootBridges (
222 PCI_ROOT_BRIDGE *Bridges,
223 UINTN Count
224 )
225 {
226 if (Bridges == NULL && Count == 0) {
227 return;
228 }
229 ASSERT (Bridges != NULL && Count > 0);
230
231 do {
232 --Count;
233 UninitRootBridge (&Bridges[Count]);
234 } while (Count > 0);
235
236 FreePool (Bridges);
237 }
238
239
240 /**
241 Inform the platform that the resource conflict happens.
242
243 @param HostBridgeHandle Handle of the Host Bridge.
244 @param Configuration Pointer to PCI I/O and PCI memory resource
245 descriptors. The Configuration contains the resources
246 for all the root bridges. The resource for each root
247 bridge is terminated with END descriptor and an
248 additional END is appended indicating the end of the
249 entire resources. The resource descriptor field
250 values follow the description in
251 EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
252 .SubmitResources().
253 **/
254 VOID
255 EFIAPI
256 PciHostBridgeResourceConflict (
257 EFI_HANDLE HostBridgeHandle,
258 VOID *Configuration
259 )
260 {
261 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Descriptor;
262 UINTN RootBridgeIndex;
263 DEBUG ((EFI_D_ERROR, "PciHostBridge: Resource conflict happens!\n"));
264
265 RootBridgeIndex = 0;
266 Descriptor = (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *) Configuration;
267 while (Descriptor->Desc == ACPI_ADDRESS_SPACE_DESCRIPTOR) {
268 DEBUG ((EFI_D_ERROR, "RootBridge[%d]:\n", RootBridgeIndex++));
269 for (; Descriptor->Desc == ACPI_ADDRESS_SPACE_DESCRIPTOR; Descriptor++) {
270 ASSERT (Descriptor->ResType <
271 (sizeof (mPciHostBridgeLibAcpiAddressSpaceTypeStr) /
272 sizeof (mPciHostBridgeLibAcpiAddressSpaceTypeStr[0])
273 )
274 );
275 DEBUG ((EFI_D_ERROR, " %s: Length/Alignment = 0x%lx / 0x%lx\n",
276 mPciHostBridgeLibAcpiAddressSpaceTypeStr[Descriptor->ResType],
277 Descriptor->AddrLen, Descriptor->AddrRangeMax
278 ));
279 if (Descriptor->ResType == ACPI_ADDRESS_SPACE_TYPE_MEM) {
280 DEBUG ((EFI_D_ERROR, " Granularity/SpecificFlag = %ld / %02x%s\n",
281 Descriptor->AddrSpaceGranularity, Descriptor->SpecificFlag,
282 ((Descriptor->SpecificFlag &
283 EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE
284 ) != 0) ? L" (Prefetchable)" : L""
285 ));
286 }
287 }
288 //
289 // Skip the END descriptor for root bridge
290 //
291 ASSERT (Descriptor->Desc == ACPI_END_TAG_DESCRIPTOR);
292 Descriptor = (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *)(
293 (EFI_ACPI_END_TAG_DESCRIPTOR *)Descriptor + 1
294 );
295 }
296 }