2 Scan the entire PCI bus for root bridges to support OVMF above Xen.
4 Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include <IndustryStandard/Pci.h> // EFI_PCI_COMMAND_IO_SPACE
11 #include <IndustryStandard/Q35MchIch9.h> // INTEL_Q35_MCH_DEVICE_ID
12 #include <Library/BaseLib.h> // DisableInterrupts()
13 #include <Library/BaseMemoryLib.h> // ZeroMem()
14 #include <Library/DebugLib.h> // ASSERT()
15 #include <Library/MemoryAllocationLib.h> // ReallocatePool()
16 #include <Library/PcdLib.h> // PcdGet16()
17 #include <Library/PciHostBridgeLib.h> // PCI_ROOT_BRIDGE_APERTURE
18 #include <Library/PciHostBridgeUtilityLib.h> // PciHostBridgeUtilityInitRoot...
19 #include <Library/PciLib.h> // PciRead32()
20 #include <Protocol/PciRootBridgeIo.h> // EFI_PCI_ATTRIBUTE_ISA_IO
22 #include "PciHostBridge.h"
26 PcatPciRootBridgeBarExisted (
28 OUT UINT32
*OriginalValue
,
33 // Preserve the original value
35 *OriginalValue
= PciRead32 (Address
);
38 // Disable timer interrupt while the BAR is probed
42 PciWrite32 (Address
, 0xFFFFFFFF);
43 *Value
= PciRead32 (Address
);
44 PciWrite32 (Address
, *OriginalValue
);
52 #define PCI_COMMAND_DECODE ((UINT16)(EFI_PCI_COMMAND_IO_SPACE | \
53 EFI_PCI_COMMAND_MEMORY_SPACE))
56 PcatPciRootBridgeDecodingDisable (
62 Value
= PciRead16 (Address
);
63 if (Value
& PCI_COMMAND_DECODE
) {
64 PciWrite16 (Address
, Value
& ~(UINT32
)PCI_COMMAND_DECODE
);
70 PcatPciRootBridgeParseBars (
75 IN UINTN BarOffsetBase
,
76 IN UINTN BarOffsetEnd
,
77 IN PCI_ROOT_BRIDGE_APERTURE
*Io
,
78 IN PCI_ROOT_BRIDGE_APERTURE
*Mem
,
79 IN PCI_ROOT_BRIDGE_APERTURE
*MemAbove4G
85 UINT32 OriginalUpperValue
;
92 PCI_ROOT_BRIDGE_APERTURE
*MemAperture
;
94 // Disable address decoding for every device before OVMF starts sizing it
95 PcatPciRootBridgeDecodingDisable (
96 PCI_LIB_ADDRESS (Bus
, Device
, Function
, PCI_COMMAND_OFFSET
)
99 for (Offset
= BarOffsetBase
; Offset
< BarOffsetEnd
; Offset
+= sizeof (UINT32
)) {
100 PcatPciRootBridgeBarExisted (
101 PCI_LIB_ADDRESS (Bus
, Device
, Function
, Offset
),
102 &OriginalValue
, &Value
107 if ((Value
& BIT0
) == BIT0
) {
111 if (Command
& EFI_PCI_COMMAND_IO_SPACE
) {
113 Base
= OriginalValue
& Mask
;
114 Length
= ((~(Value
& Mask
)) & Mask
) + 0x04;
115 if (!(Value
& 0xFFFF0000)) {
116 Length
&= 0x0000FFFF;
118 Limit
= Base
+ Length
- 1;
121 if (Io
->Base
> Base
) {
124 if (Io
->Limit
< Limit
) {
133 if (Command
& EFI_PCI_COMMAND_MEMORY_SPACE
) {
136 Base
= OriginalValue
& Mask
;
137 Length
= Value
& Mask
;
139 if ((Value
& (BIT1
| BIT2
)) == 0) {
143 Length
= ((~Length
) + 1) & 0xffffffff;
151 PcatPciRootBridgeBarExisted (
152 PCI_LIB_ADDRESS (Bus
, Device
, Function
, Offset
),
157 Base
= Base
| LShiftU64 ((UINT64
) OriginalUpperValue
, 32);
158 Length
= Length
| LShiftU64 ((UINT64
) UpperValue
, 32);
159 Length
= (~Length
) + 1;
161 if (Base
< BASE_4GB
) {
164 MemAperture
= MemAbove4G
;
168 Limit
= Base
+ Length
- 1;
170 if (MemAperture
->Base
> Base
) {
171 MemAperture
->Base
= Base
;
173 if (MemAperture
->Limit
< Limit
) {
174 MemAperture
->Limit
= Limit
;
182 STATIC PCI_ROOT_BRIDGE_APERTURE mNonExistAperture
= { MAX_UINT64
, 0 };
186 UINTN
*NumberOfRootBridges
193 UINTN NumberOfDevices
;
200 PCI_ROOT_BRIDGE_APERTURE Io
, Mem
, MemAbove4G
, *MemAperture
;
201 PCI_ROOT_BRIDGE
*RootBridges
;
205 *NumberOfRootBridges
= 0;
209 // After scanning all the PCI devices on the PCI root bridge's primary bus,
210 // update the Primary Bus Number for the next PCI root bridge to be this PCI
211 // root bridge's subordinate bus number + 1.
213 for (PrimaryBus
= 0; PrimaryBus
<= PCI_MAX_BUS
; PrimaryBus
= SubBus
+ 1) {
217 ZeroMem (&Io
, sizeof (Io
));
218 ZeroMem (&Mem
, sizeof (Mem
));
219 ZeroMem (&MemAbove4G
, sizeof (MemAbove4G
));
220 Io
.Base
= Mem
.Base
= MemAbove4G
.Base
= MAX_UINT64
;
222 // Scan all the PCI devices on the primary bus of the PCI root bridge
224 for (Device
= 0, NumberOfDevices
= 0; Device
<= PCI_MAX_DEVICE
; Device
++) {
226 for (Function
= 0; Function
<= PCI_MAX_FUNC
; Function
++) {
229 // Compute the PCI configuration address of the PCI device to probe
231 Address
= PCI_LIB_ADDRESS (PrimaryBus
, Device
, Function
, 0);
234 // Read the Vendor ID from the PCI Configuration Header
236 if (PciRead16 (Address
) == MAX_UINT16
) {
239 // If the PCI Configuration Read fails, or a PCI device does not
240 // exist, then skip this entire PCI device
245 // If PCI function != 0, VendorId == 0xFFFF, we continue to search
253 // Read the entire PCI Configuration Header
255 PciReadBuffer (Address
, sizeof (Pci
), &Pci
);
258 // Increment the number of PCI device found on the primary bus of the
264 // Look for devices with the VGA Palette Snoop enabled in the COMMAND
265 // register of the PCI Config Header
267 if ((Pci
.Hdr
.Command
& EFI_PCI_COMMAND_VGA_PALETTE_SNOOP
) != 0) {
268 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO
;
269 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO_16
;
277 if (IS_PCI_BRIDGE (&Pci
)) {
279 // Get the Bus range that the PPB is decoding
281 if (Pci
.Bridge
.SubordinateBus
> SubBus
) {
283 // If the subordinate bus number of the PCI-PCI bridge is greater
284 // than the PCI root bridge's current subordinate bus number,
285 // then update the PCI root bridge's subordinate bus number
287 SubBus
= Pci
.Bridge
.SubordinateBus
;
291 // Get the I/O range that the PPB is decoding
293 Value
= Pci
.Bridge
.IoBase
& 0x0f;
294 Base
= ((UINT32
) Pci
.Bridge
.IoBase
& 0xf0) << 8;
295 Limit
= (((UINT32
) Pci
.Bridge
.IoLimit
& 0xf0) << 8) | 0x0fff;
297 Base
|= ((UINT32
) Pci
.Bridge
.IoBaseUpper16
<< 16);
298 Limit
|= ((UINT32
) Pci
.Bridge
.IoLimitUpper16
<< 16);
301 if (Io
.Base
> Base
) {
304 if (Io
.Limit
< Limit
) {
310 // Get the Memory range that the PPB is decoding
312 Base
= ((UINT32
) Pci
.Bridge
.MemoryBase
& 0xfff0) << 16;
313 Limit
= (((UINT32
) Pci
.Bridge
.MemoryLimit
& 0xfff0) << 16) | 0xfffff;
315 if (Mem
.Base
> Base
) {
318 if (Mem
.Limit
< Limit
) {
324 // Get the Prefetchable Memory range that the PPB is decoding
325 // and merge it into Memory range
327 Value
= Pci
.Bridge
.PrefetchableMemoryBase
& 0x0f;
328 Base
= ((UINT32
) Pci
.Bridge
.PrefetchableMemoryBase
& 0xfff0) << 16;
329 Limit
= (((UINT32
) Pci
.Bridge
.PrefetchableMemoryLimit
& 0xfff0)
333 Base
|= LShiftU64 (Pci
.Bridge
.PrefetchableBaseUpper32
, 32);
334 Limit
|= LShiftU64 (Pci
.Bridge
.PrefetchableLimitUpper32
, 32);
335 MemAperture
= &MemAbove4G
;
338 if (MemAperture
->Base
> Base
) {
339 MemAperture
->Base
= Base
;
341 if (MemAperture
->Limit
< Limit
) {
342 MemAperture
->Limit
= Limit
;
347 // Look at the PPB Configuration for legacy decoding attributes
349 if ((Pci
.Bridge
.BridgeControl
& EFI_PCI_BRIDGE_CONTROL_ISA
)
350 == EFI_PCI_BRIDGE_CONTROL_ISA
) {
351 Attributes
|= EFI_PCI_ATTRIBUTE_ISA_IO
;
352 Attributes
|= EFI_PCI_ATTRIBUTE_ISA_IO_16
;
353 Attributes
|= EFI_PCI_ATTRIBUTE_ISA_MOTHERBOARD_IO
;
355 if ((Pci
.Bridge
.BridgeControl
& EFI_PCI_BRIDGE_CONTROL_VGA
)
356 == EFI_PCI_BRIDGE_CONTROL_VGA
) {
357 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO
;
358 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_MEMORY
;
359 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_IO
;
360 if ((Pci
.Bridge
.BridgeControl
& EFI_PCI_BRIDGE_CONTROL_VGA_16
)
362 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO_16
;
363 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_IO_16
;
367 BarOffsetEnd
= OFFSET_OF (PCI_TYPE01
, Bridge
.Bar
[2]);
370 // Parse the BARs of the PCI device to get what I/O Ranges, Memory
371 // Ranges, and Prefetchable Memory Ranges the device is decoding
373 if ((Pci
.Hdr
.HeaderType
& HEADER_LAYOUT_CODE
) == HEADER_TYPE_DEVICE
) {
374 BarOffsetEnd
= OFFSET_OF (PCI_TYPE00
, Device
.Bar
[6]);
378 PcatPciRootBridgeParseBars (
383 OFFSET_OF (PCI_TYPE00
, Device
.Bar
),
390 // See if the PCI device is an IDE controller
392 if (IS_CLASS2 (&Pci
, PCI_CLASS_MASS_STORAGE
,
393 PCI_CLASS_MASS_STORAGE_IDE
)) {
394 if (Pci
.Hdr
.ClassCode
[0] & 0x80) {
395 Attributes
|= EFI_PCI_ATTRIBUTE_IDE_PRIMARY_IO
;
396 Attributes
|= EFI_PCI_ATTRIBUTE_IDE_SECONDARY_IO
;
398 if (Pci
.Hdr
.ClassCode
[0] & 0x01) {
399 Attributes
|= EFI_PCI_ATTRIBUTE_IDE_PRIMARY_IO
;
401 if (Pci
.Hdr
.ClassCode
[0] & 0x04) {
402 Attributes
|= EFI_PCI_ATTRIBUTE_IDE_SECONDARY_IO
;
407 // See if the PCI device is a legacy VGA controller or
408 // a standard VGA controller
410 if (IS_CLASS2 (&Pci
, PCI_CLASS_OLD
, PCI_CLASS_OLD_VGA
) ||
411 IS_CLASS2 (&Pci
, PCI_CLASS_DISPLAY
, PCI_CLASS_DISPLAY_VGA
)
413 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO
;
414 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO_16
;
415 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_MEMORY
;
416 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_IO
;
417 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_IO_16
;
421 // See if the PCI Device is a PCI - ISA or PCI - EISA
422 // or ISA_POSITIVE_DECODE Bridge device
424 if (Pci
.Hdr
.ClassCode
[2] == PCI_CLASS_BRIDGE
) {
425 if (Pci
.Hdr
.ClassCode
[1] == PCI_CLASS_BRIDGE_ISA
||
426 Pci
.Hdr
.ClassCode
[1] == PCI_CLASS_BRIDGE_EISA
||
427 Pci
.Hdr
.ClassCode
[1] == PCI_CLASS_BRIDGE_ISA_PDECODE
) {
428 Attributes
|= EFI_PCI_ATTRIBUTE_ISA_IO
;
429 Attributes
|= EFI_PCI_ATTRIBUTE_ISA_IO_16
;
430 Attributes
|= EFI_PCI_ATTRIBUTE_ISA_MOTHERBOARD_IO
;
435 // If this device is not a multi function device, then skip the rest
436 // of this PCI device
438 if (Function
== 0 && !IS_PCI_MULTI_FUNC (&Pci
)) {
445 // If at least one PCI device was found on the primary bus of this PCI
446 // root bridge, then the PCI root bridge exists.
448 if (NumberOfDevices
> 0) {
449 RootBridges
= ReallocatePool (
450 (*NumberOfRootBridges
) * sizeof (PCI_ROOT_BRIDGE
),
451 (*NumberOfRootBridges
+ 1) * sizeof (PCI_ROOT_BRIDGE
),
454 ASSERT (RootBridges
!= NULL
);
455 PciHostBridgeUtilityInitRootBridge (
456 Attributes
, Attributes
, 0,
457 FALSE
, PcdGet16 (PcdOvmfHostBridgePciDevId
) != INTEL_Q35_MCH_DEVICE_ID
,
458 (UINT8
) PrimaryBus
, (UINT8
) SubBus
,
459 &Io
, &Mem
, &MemAbove4G
, &mNonExistAperture
, &mNonExistAperture
,
460 &RootBridges
[*NumberOfRootBridges
]
462 RootBridges
[*NumberOfRootBridges
].ResourceAssigned
= TRUE
;
464 // Increment the index for the next PCI Root Bridge
466 (*NumberOfRootBridges
)++;