2 OVMF's instance of the PCI Host Bridge Library.
4 Copyright (C) 2016, Red Hat, Inc.
5 Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
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.
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.
18 #include <IndustryStandard/Pci.h>
20 #include <Protocol/PciRootBridgeIo.h>
22 #include <Library/BaseMemoryLib.h>
23 #include <Library/DebugLib.h>
24 #include <Library/MemoryAllocationLib.h>
25 #include <Library/PciHostBridgeLib.h>
26 #include <Library/PciLib.h>
27 #include <Library/QemuFwCfgLib.h>
30 GLOBAL_REMOVE_IF_UNREFERENCED
31 CHAR16
*mPciHostBridgeLibAcpiAddressSpaceTypeStr
[] = {
32 L
"Mem", L
"I/O", L
"Bus"
37 Initialize a PCI_ROOT_BRIDGE structure.
39 param[in] RootBusNumber The bus number to store in RootBus.
41 param[in] MaxSubBusNumber The inclusive maximum bus number that can be
42 assigned to any subordinate bus found behind any
43 PCI bridge hanging off this root bus.
45 The caller is repsonsible for ensuring that
46 RootBusNumber <= MaxSubBusNumber. If
47 RootBusNumber equals MaxSubBusNumber, then the
48 root bus has no room for subordinate buses.
50 param[out] RootBus The PCI_ROOT_BRIDGE structure (allocated by the
51 caller) that should be filled in by this
54 @retval EFI_SUCCESS Initialization successful. A device path
55 consisting of an ACPI device path node, with
56 UID = RootBusNumber, has been allocated and
59 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
64 IN UINT8 RootBusNumber
,
65 IN UINT8 MaxSubBusNumber
,
66 OUT PCI_ROOT_BRIDGE
*RootBus
70 // Be safe if other fields are added to PCI_ROOT_BRIDGE later.
72 ZeroMem (RootBus
, sizeof *RootBus
);
76 RootBus
->Supports
= EFI_PCI_ATTRIBUTE_IDE_PRIMARY_IO
|
77 EFI_PCI_ATTRIBUTE_IDE_SECONDARY_IO
|
78 EFI_PCI_ATTRIBUTE_ISA_IO_16
|
79 EFI_PCI_ATTRIBUTE_ISA_MOTHERBOARD_IO
|
80 EFI_PCI_ATTRIBUTE_VGA_MEMORY
|
81 EFI_PCI_ATTRIBUTE_VGA_IO_16
|
82 EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO_16
;
83 RootBus
->Attributes
= RootBus
->Supports
;
85 return EFI_OUT_OF_RESOURCES
;
90 Uninitialize a PCI_ROOT_BRIDGE structure set up with InitRootBridge().
92 param[in] RootBus The PCI_ROOT_BRIDGE structure, allocated by the caller and
93 initialized with InitRootBridge(), that should be
94 uninitialized. This function doesn't free RootBus.
99 IN PCI_ROOT_BRIDGE
*RootBus
106 Return all the root bridge instances in an array.
108 @param Count Return the count of root bridge instances.
110 @return All the root bridge instances in an array.
111 The array should be passed into PciHostBridgeFreeRootBridges()
116 PciHostBridgeGetRootBridges (
121 FIRMWARE_CONFIG_ITEM FwCfgItem
;
123 UINT64 ExtraRootBridges
;
124 PCI_ROOT_BRIDGE
*Bridges
;
126 UINTN LastRootBridgeNumber
;
127 UINTN RootBridgeNumber
;
132 // QEMU provides the number of extra root buses, shortening the exhaustive
133 // search below. If there is no hint, the feature is missing.
135 Status
= QemuFwCfgFindFile ("etc/extra-pci-roots", &FwCfgItem
, &FwCfgSize
);
136 if (EFI_ERROR (Status
) || FwCfgSize
!= sizeof ExtraRootBridges
) {
137 ExtraRootBridges
= 0;
139 QemuFwCfgSelectItem (FwCfgItem
);
140 QemuFwCfgReadBytes (FwCfgSize
, &ExtraRootBridges
);
142 if (ExtraRootBridges
> PCI_MAX_BUS
) {
143 DEBUG ((EFI_D_ERROR
, "%a: invalid count of extra root buses (%Lu) "
144 "reported by QEMU\n", __FUNCTION__
, ExtraRootBridges
));
147 DEBUG ((EFI_D_INFO
, "%a: %Lu extra root buses reported by QEMU\n",
148 __FUNCTION__
, ExtraRootBridges
));
152 // Allocate the "main" root bridge, and any extra root bridges.
154 Bridges
= AllocatePool ((1 + (UINTN
)ExtraRootBridges
) * sizeof *Bridges
);
155 if (Bridges
== NULL
) {
156 DEBUG ((EFI_D_ERROR
, "%a: %r\n", __FUNCTION__
, EFI_OUT_OF_RESOURCES
));
162 // The "main" root bus is always there.
164 LastRootBridgeNumber
= 0;
167 // Scan all other root buses. If function 0 of any device on a bus returns a
168 // VendorId register value different from all-bits-one, then that bus is
171 for (RootBridgeNumber
= 1;
172 RootBridgeNumber
<= PCI_MAX_BUS
&& Initialized
< ExtraRootBridges
;
173 ++RootBridgeNumber
) {
176 for (Device
= 0; Device
<= PCI_MAX_DEVICE
; ++Device
) {
177 if (PciRead16 (PCI_LIB_ADDRESS (RootBridgeNumber
, Device
, 0,
178 PCI_VENDOR_ID_OFFSET
)) != MAX_UINT16
) {
182 if (Device
<= PCI_MAX_DEVICE
) {
184 // Found the next root bus. We can now install the *previous* one,
185 // because now we know how big a bus number range *that* one has, for any
186 // subordinate buses that might exist behind PCI bridges hanging off it.
188 Status
= InitRootBridge ((UINT8
)LastRootBridgeNumber
,
189 (UINT8
)(RootBridgeNumber
- 1), &Bridges
[Initialized
]);
190 if (EFI_ERROR (Status
)) {
194 LastRootBridgeNumber
= RootBridgeNumber
;
199 // Install the last root bus (which might be the only, ie. main, root bus, if
200 // we've found no extra root buses).
202 Status
= InitRootBridge ((UINT8
)LastRootBridgeNumber
, PCI_MAX_BUS
,
203 &Bridges
[Initialized
]);
204 if (EFI_ERROR (Status
)) {
209 *Count
= Initialized
;
213 while (Initialized
> 0) {
215 UninitRootBridge (&Bridges
[Initialized
]);
224 Free the root bridge instances array returned from
225 PciHostBridgeGetRootBridges().
227 @param The root bridge instances array.
228 @param The count of the array.
232 PciHostBridgeFreeRootBridges (
233 PCI_ROOT_BRIDGE
*Bridges
,
237 if (Bridges
== NULL
&& Count
== 0) {
240 ASSERT (Bridges
!= NULL
&& Count
> 0);
244 UninitRootBridge (&Bridges
[Count
]);
252 Inform the platform that the resource conflict happens.
254 @param HostBridgeHandle Handle of the Host Bridge.
255 @param Configuration Pointer to PCI I/O and PCI memory resource
256 descriptors. The Configuration contains the resources
257 for all the root bridges. The resource for each root
258 bridge is terminated with END descriptor and an
259 additional END is appended indicating the end of the
260 entire resources. The resource descriptor field
261 values follow the description in
262 EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
267 PciHostBridgeResourceConflict (
268 EFI_HANDLE HostBridgeHandle
,
272 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Descriptor
;
273 UINTN RootBridgeIndex
;
274 DEBUG ((EFI_D_ERROR
, "PciHostBridge: Resource conflict happens!\n"));
277 Descriptor
= (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*) Configuration
;
278 while (Descriptor
->Desc
== ACPI_ADDRESS_SPACE_DESCRIPTOR
) {
279 DEBUG ((EFI_D_ERROR
, "RootBridge[%d]:\n", RootBridgeIndex
++));
280 for (; Descriptor
->Desc
== ACPI_ADDRESS_SPACE_DESCRIPTOR
; Descriptor
++) {
281 ASSERT (Descriptor
->ResType
<
282 (sizeof (mPciHostBridgeLibAcpiAddressSpaceTypeStr
) /
283 sizeof (mPciHostBridgeLibAcpiAddressSpaceTypeStr
[0])
286 DEBUG ((EFI_D_ERROR
, " %s: Length/Alignment = 0x%lx / 0x%lx\n",
287 mPciHostBridgeLibAcpiAddressSpaceTypeStr
[Descriptor
->ResType
],
288 Descriptor
->AddrLen
, Descriptor
->AddrRangeMax
290 if (Descriptor
->ResType
== ACPI_ADDRESS_SPACE_TYPE_MEM
) {
291 DEBUG ((EFI_D_ERROR
, " Granularity/SpecificFlag = %ld / %02x%s\n",
292 Descriptor
->AddrSpaceGranularity
, Descriptor
->SpecificFlag
,
293 ((Descriptor
->SpecificFlag
&
294 EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE
295 ) != 0) ? L
" (Prefetchable)" : L
""
300 // Skip the END descriptor for root bridge
302 ASSERT (Descriptor
->Desc
== ACPI_END_TAG_DESCRIPTOR
);
303 Descriptor
= (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*)(
304 (EFI_ACPI_END_TAG_DESCRIPTOR
*)Descriptor
+ 1