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
11 #include <IndustryStandard/Pci.h>
12 #include <IndustryStandard/Q35MchIch9.h>
14 #include <Protocol/PciHostBridgeResourceAllocation.h>
15 #include <Protocol/PciRootBridgeIo.h>
17 #include <Library/BaseMemoryLib.h>
18 #include <Library/DebugLib.h>
19 #include <Library/MemoryAllocationLib.h>
20 #include <Library/PciHostBridgeLib.h>
21 #include <Library/PciLib.h>
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
);
54 PcatPciRootBridgeParseBars (
59 IN UINTN BarOffsetBase
,
60 IN UINTN BarOffsetEnd
,
61 IN PCI_ROOT_BRIDGE_APERTURE
*Io
,
62 IN PCI_ROOT_BRIDGE_APERTURE
*Mem
,
63 IN PCI_ROOT_BRIDGE_APERTURE
*MemAbove4G
69 UINT32 OriginalUpperValue
;
76 PCI_ROOT_BRIDGE_APERTURE
*MemAperture
;
78 for (Offset
= BarOffsetBase
; Offset
< BarOffsetEnd
; Offset
+= sizeof (UINT32
)) {
79 PcatPciRootBridgeBarExisted (
80 PCI_LIB_ADDRESS (Bus
, Device
, Function
, Offset
),
81 &OriginalValue
, &Value
86 if ((Value
& BIT0
) == BIT0
) {
90 if (Command
& EFI_PCI_COMMAND_IO_SPACE
) {
92 Base
= OriginalValue
& Mask
;
93 Length
= ((~(Value
& Mask
)) & Mask
) + 0x04;
94 if (!(Value
& 0xFFFF0000)) {
97 Limit
= Base
+ Length
- 1;
100 if (Io
->Base
> Base
) {
103 if (Io
->Limit
< Limit
) {
112 if (Command
& EFI_PCI_COMMAND_MEMORY_SPACE
) {
115 Base
= OriginalValue
& Mask
;
116 Length
= Value
& Mask
;
118 if ((Value
& (BIT1
| BIT2
)) == 0) {
122 Length
= ((~Length
) + 1) & 0xffffffff;
130 PcatPciRootBridgeBarExisted (
131 PCI_LIB_ADDRESS (Bus
, Device
, Function
, Offset
),
136 Base
= Base
| LShiftU64 ((UINT64
) OriginalUpperValue
, 32);
137 Length
= Length
| LShiftU64 ((UINT64
) UpperValue
, 32);
138 Length
= (~Length
) + 1;
140 MemAperture
= MemAbove4G
;
143 Limit
= Base
+ Length
- 1;
145 if (MemAperture
->Base
> Base
) {
146 MemAperture
->Base
= Base
;
148 if (MemAperture
->Limit
< Limit
) {
149 MemAperture
->Limit
= Limit
;
157 STATIC PCI_ROOT_BRIDGE_APERTURE mNonExistAperture
= { MAX_UINT64
, 0 };
161 UINTN
*NumberOfRootBridges
168 UINTN NumberOfDevices
;
175 PCI_ROOT_BRIDGE_APERTURE Io
, Mem
, MemAbove4G
, *MemAperture
;
176 PCI_ROOT_BRIDGE
*RootBridges
;
180 *NumberOfRootBridges
= 0;
184 // After scanning all the PCI devices on the PCI root bridge's primary bus,
185 // update the Primary Bus Number for the next PCI root bridge to be this PCI
186 // root bridge's subordinate bus number + 1.
188 for (PrimaryBus
= 0; PrimaryBus
<= PCI_MAX_BUS
; PrimaryBus
= SubBus
+ 1) {
192 ZeroMem (&Io
, sizeof (Io
));
193 ZeroMem (&Mem
, sizeof (Mem
));
194 ZeroMem (&MemAbove4G
, sizeof (MemAbove4G
));
195 Io
.Base
= Mem
.Base
= MemAbove4G
.Base
= MAX_UINT64
;
197 // Scan all the PCI devices on the primary bus of the PCI root bridge
199 for (Device
= 0, NumberOfDevices
= 0; Device
<= PCI_MAX_DEVICE
; Device
++) {
201 for (Function
= 0; Function
<= PCI_MAX_FUNC
; Function
++) {
204 // Compute the PCI configuration address of the PCI device to probe
206 Address
= PCI_LIB_ADDRESS (PrimaryBus
, Device
, Function
, 0);
209 // Read the Vendor ID from the PCI Configuration Header
211 if (PciRead16 (Address
) == MAX_UINT16
) {
214 // If the PCI Configuration Read fails, or a PCI device does not
215 // exist, then skip this entire PCI device
220 // If PCI function != 0, VendorId == 0xFFFF, we continue to search
228 // Read the entire PCI Configuration Header
230 PciReadBuffer (Address
, sizeof (Pci
), &Pci
);
233 // Increment the number of PCI device found on the primary bus of the
239 // Look for devices with the VGA Palette Snoop enabled in the COMMAND
240 // register of the PCI Config Header
242 if ((Pci
.Hdr
.Command
& EFI_PCI_COMMAND_VGA_PALETTE_SNOOP
) != 0) {
243 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO
;
244 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO_16
;
252 if (IS_PCI_BRIDGE (&Pci
)) {
254 // Get the Bus range that the PPB is decoding
256 if (Pci
.Bridge
.SubordinateBus
> SubBus
) {
258 // If the suborinate bus number of the PCI-PCI bridge is greater
259 // than the PCI root bridge's current subordinate bus number,
260 // then update the PCI root bridge's subordinate bus number
262 SubBus
= Pci
.Bridge
.SubordinateBus
;
266 // Get the I/O range that the PPB is decoding
268 Value
= Pci
.Bridge
.IoBase
& 0x0f;
269 Base
= ((UINT32
) Pci
.Bridge
.IoBase
& 0xf0) << 8;
270 Limit
= (((UINT32
) Pci
.Bridge
.IoLimit
& 0xf0) << 8) | 0x0fff;
272 Base
|= ((UINT32
) Pci
.Bridge
.IoBaseUpper16
<< 16);
273 Limit
|= ((UINT32
) Pci
.Bridge
.IoLimitUpper16
<< 16);
276 if (Io
.Base
> Base
) {
279 if (Io
.Limit
< Limit
) {
285 // Get the Memory range that the PPB is decoding
287 Base
= ((UINT32
) Pci
.Bridge
.MemoryBase
& 0xfff0) << 16;
288 Limit
= (((UINT32
) Pci
.Bridge
.MemoryLimit
& 0xfff0) << 16) | 0xfffff;
290 if (Mem
.Base
> Base
) {
293 if (Mem
.Limit
< Limit
) {
299 // Get the Prefetchable Memory range that the PPB is decoding
300 // and merge it into Memory range
302 Value
= Pci
.Bridge
.PrefetchableMemoryBase
& 0x0f;
303 Base
= ((UINT32
) Pci
.Bridge
.PrefetchableMemoryBase
& 0xfff0) << 16;
304 Limit
= (((UINT32
) Pci
.Bridge
.PrefetchableMemoryLimit
& 0xfff0)
308 Base
|= LShiftU64 (Pci
.Bridge
.PrefetchableBaseUpper32
, 32);
309 Limit
|= LShiftU64 (Pci
.Bridge
.PrefetchableLimitUpper32
, 32);
310 MemAperture
= &MemAbove4G
;
313 if (MemAperture
->Base
> Base
) {
314 MemAperture
->Base
= Base
;
316 if (MemAperture
->Limit
< Limit
) {
317 MemAperture
->Limit
= Limit
;
322 // Look at the PPB Configuration for legacy decoding attributes
324 if ((Pci
.Bridge
.BridgeControl
& EFI_PCI_BRIDGE_CONTROL_ISA
)
325 == EFI_PCI_BRIDGE_CONTROL_ISA
) {
326 Attributes
|= EFI_PCI_ATTRIBUTE_ISA_IO
;
327 Attributes
|= EFI_PCI_ATTRIBUTE_ISA_IO_16
;
328 Attributes
|= EFI_PCI_ATTRIBUTE_ISA_MOTHERBOARD_IO
;
330 if ((Pci
.Bridge
.BridgeControl
& EFI_PCI_BRIDGE_CONTROL_VGA
)
331 == EFI_PCI_BRIDGE_CONTROL_VGA
) {
332 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO
;
333 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_MEMORY
;
334 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_IO
;
335 if ((Pci
.Bridge
.BridgeControl
& EFI_PCI_BRIDGE_CONTROL_VGA_16
)
337 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO_16
;
338 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_IO_16
;
342 BarOffsetEnd
= OFFSET_OF (PCI_TYPE01
, Bridge
.Bar
[2]);
345 // Parse the BARs of the PCI device to get what I/O Ranges, Memory
346 // Ranges, and Prefetchable Memory Ranges the device is decoding
348 if ((Pci
.Hdr
.HeaderType
& HEADER_LAYOUT_CODE
) == HEADER_TYPE_DEVICE
) {
349 BarOffsetEnd
= OFFSET_OF (PCI_TYPE00
, Device
.Bar
[6]);
353 PcatPciRootBridgeParseBars (
358 OFFSET_OF (PCI_TYPE00
, Device
.Bar
),
365 // See if the PCI device is an IDE controller
367 if (IS_CLASS2 (&Pci
, PCI_CLASS_MASS_STORAGE
,
368 PCI_CLASS_MASS_STORAGE_IDE
)) {
369 if (Pci
.Hdr
.ClassCode
[0] & 0x80) {
370 Attributes
|= EFI_PCI_ATTRIBUTE_IDE_PRIMARY_IO
;
371 Attributes
|= EFI_PCI_ATTRIBUTE_IDE_SECONDARY_IO
;
373 if (Pci
.Hdr
.ClassCode
[0] & 0x01) {
374 Attributes
|= EFI_PCI_ATTRIBUTE_IDE_PRIMARY_IO
;
376 if (Pci
.Hdr
.ClassCode
[0] & 0x04) {
377 Attributes
|= EFI_PCI_ATTRIBUTE_IDE_SECONDARY_IO
;
382 // See if the PCI device is a legacy VGA controller or
383 // a standard VGA controller
385 if (IS_CLASS2 (&Pci
, PCI_CLASS_OLD
, PCI_CLASS_OLD_VGA
) ||
386 IS_CLASS2 (&Pci
, PCI_CLASS_DISPLAY
, PCI_CLASS_DISPLAY_VGA
)
388 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO
;
389 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO_16
;
390 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_MEMORY
;
391 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_IO
;
392 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_IO_16
;
396 // See if the PCI Device is a PCI - ISA or PCI - EISA
397 // or ISA_POSITIVIE_DECODE Bridge device
399 if (Pci
.Hdr
.ClassCode
[2] == PCI_CLASS_BRIDGE
) {
400 if (Pci
.Hdr
.ClassCode
[1] == PCI_CLASS_BRIDGE_ISA
||
401 Pci
.Hdr
.ClassCode
[1] == PCI_CLASS_BRIDGE_EISA
||
402 Pci
.Hdr
.ClassCode
[1] == PCI_CLASS_BRIDGE_ISA_PDECODE
) {
403 Attributes
|= EFI_PCI_ATTRIBUTE_ISA_IO
;
404 Attributes
|= EFI_PCI_ATTRIBUTE_ISA_IO_16
;
405 Attributes
|= EFI_PCI_ATTRIBUTE_ISA_MOTHERBOARD_IO
;
410 // If this device is not a multi function device, then skip the rest
411 // of this PCI device
413 if (Function
== 0 && !IS_PCI_MULTI_FUNC (&Pci
)) {
420 // If at least one PCI device was found on the primary bus of this PCI
421 // root bridge, then the PCI root bridge exists.
423 if (NumberOfDevices
> 0) {
424 RootBridges
= ReallocatePool (
425 (*NumberOfRootBridges
) * sizeof (PCI_ROOT_BRIDGE
),
426 (*NumberOfRootBridges
+ 1) * sizeof (PCI_ROOT_BRIDGE
),
429 ASSERT (RootBridges
!= NULL
);
431 Attributes
, Attributes
, 0,
432 (UINT8
) PrimaryBus
, (UINT8
) SubBus
,
433 &Io
, &Mem
, &MemAbove4G
, &mNonExistAperture
, &mNonExistAperture
,
434 &RootBridges
[*NumberOfRootBridges
]
436 RootBridges
[*NumberOfRootBridges
].ResourceAssigned
= TRUE
;
438 // Increment the index for the next PCI Root Bridge
440 (*NumberOfRootBridges
)++;