2 Scan the entire PCI bus for root bridges to support coreboot UEFI payload.
4 Copyright (c) 2016 - 2021, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <IndustryStandard/Pci.h>
12 #include <Protocol/PciHostBridgeResourceAllocation.h>
13 #include <Protocol/PciRootBridgeIo.h>
14 #include <Library/BaseMemoryLib.h>
15 #include <Library/DebugLib.h>
16 #include <Library/MemoryAllocationLib.h>
17 #include <Library/PciHostBridgeLib.h>
18 #include <Library/PciLib.h>
19 #include "PciHostBridge.h"
22 Adjust the collected PCI resource.
24 @param[in] Io IO aperture.
26 @param[in] Mem MMIO aperture.
28 @param[in] MemAbove4G MMIO aperture above 4G.
30 @param[in] PMem Prefetchable MMIO aperture.
32 @param[in] PMemAbove4G Prefetchable MMIO aperture above 4G.
35 AdjustRootBridgeResource (
36 IN PCI_ROOT_BRIDGE_APERTURE
*Io
,
37 IN PCI_ROOT_BRIDGE_APERTURE
*Mem
,
38 IN PCI_ROOT_BRIDGE_APERTURE
*MemAbove4G
,
39 IN PCI_ROOT_BRIDGE_APERTURE
*PMem
,
40 IN PCI_ROOT_BRIDGE_APERTURE
*PMemAbove4G
46 // For now try to downgrade everything into MEM32 since
47 // - coreboot does not assign resource above 4GB
48 // - coreboot might allocate interleaved MEM32 and PMEM32 resource
51 if (PMem
->Base
< Mem
->Base
) {
52 Mem
->Base
= PMem
->Base
;
55 if (PMem
->Limit
> Mem
->Limit
) {
56 Mem
->Limit
= PMem
->Limit
;
59 PMem
->Base
= MAX_UINT64
;
62 if (MemAbove4G
->Base
< 0x100000000ULL
) {
63 if (MemAbove4G
->Base
< Mem
->Base
) {
64 Mem
->Base
= MemAbove4G
->Base
;
67 if (MemAbove4G
->Limit
> Mem
->Limit
) {
68 Mem
->Limit
= MemAbove4G
->Limit
;
71 MemAbove4G
->Base
= MAX_UINT64
;
72 MemAbove4G
->Limit
= 0;
75 if (PMemAbove4G
->Base
< 0x100000000ULL
) {
76 if (PMemAbove4G
->Base
< Mem
->Base
) {
77 Mem
->Base
= PMemAbove4G
->Base
;
80 if (PMemAbove4G
->Limit
> Mem
->Limit
) {
81 Mem
->Limit
= PMemAbove4G
->Limit
;
84 PMemAbove4G
->Base
= MAX_UINT64
;
85 PMemAbove4G
->Limit
= 0;
89 // Align IO resource at 4K boundary
92 Io
->Limit
= ((Io
->Limit
+ Mask
) & ~Mask
) - 1;
93 if (Io
->Base
!= MAX_UINT64
) {
98 // Align MEM resource at 1MB boundary
101 Mem
->Limit
= ((Mem
->Limit
+ Mask
) & ~Mask
) - 1;
102 if (Mem
->Base
!= MAX_UINT64
) {
108 Probe a bar is existed or not.
110 @param[in] Address PCI address for the BAR.
111 @param[out] OriginalValue The original bar value returned.
112 @param[out] Value The probed bar value returned.
116 PcatPciRootBridgeBarExisted (
118 OUT UINT32
*OriginalValue
,
124 PciAddress
= (UINTN
)Address
;
127 // Preserve the original value
129 *OriginalValue
= PciRead32 (PciAddress
);
132 // Disable timer interrupt while the BAR is probed
134 DisableInterrupts ();
136 PciWrite32 (PciAddress
, 0xFFFFFFFF);
137 *Value
= PciRead32 (PciAddress
);
138 PciWrite32 (PciAddress
, *OriginalValue
);
147 Parse PCI bar and collect the assigned PCI resource information.
149 @param[in] Command Supported attributes.
151 @param[in] Bus PCI bus number.
153 @param[in] Device PCI device number.
155 @param[in] Function PCI function number.
157 @param[in] BarOffsetBase PCI bar start offset.
159 @param[in] BarOffsetEnd PCI bar end offset.
161 @param[in] Io IO aperture.
163 @param[in] Mem MMIO aperture.
165 @param[in] MemAbove4G MMIO aperture above 4G.
167 @param[in] PMem Prefetchable MMIO aperture.
169 @param[in] PMemAbove4G Prefetchable MMIO aperture above 4G.
173 PcatPciRootBridgeParseBars (
178 IN UINTN BarOffsetBase
,
179 IN UINTN BarOffsetEnd
,
180 IN PCI_ROOT_BRIDGE_APERTURE
*Io
,
181 IN PCI_ROOT_BRIDGE_APERTURE
*Mem
,
182 IN PCI_ROOT_BRIDGE_APERTURE
*MemAbove4G
,
183 IN PCI_ROOT_BRIDGE_APERTURE
*PMem
,
184 IN PCI_ROOT_BRIDGE_APERTURE
*PMemAbove4G
188 UINT32 OriginalValue
;
190 UINT32 OriginalUpperValue
;
198 PCI_ROOT_BRIDGE_APERTURE
*MemAperture
;
200 for (Offset
= BarOffsetBase
; Offset
< BarOffsetEnd
; Offset
+= sizeof (UINT32
)) {
201 PcatPciRootBridgeBarExisted (
202 PCI_LIB_ADDRESS (Bus
, Device
, Function
, Offset
),
210 if ((Value
& BIT0
) == BIT0
) {
214 if ((Command
& EFI_PCI_COMMAND_IO_SPACE
) != 0) {
216 Base
= OriginalValue
& Mask
;
217 Length
= ((~(Value
& Mask
)) & Mask
) + 0x04;
218 if (!(Value
& 0xFFFF0000)) {
219 Length
&= 0x0000FFFF;
222 Limit
= Base
+ Length
- 1;
224 if ((Base
> 0) && (Base
< Limit
)) {
225 if (Io
->Base
> Base
) {
229 if (Io
->Limit
< Limit
) {
238 if ((Command
& EFI_PCI_COMMAND_MEMORY_SPACE
) != 0) {
240 Base
= OriginalValue
& Mask
;
241 Length
= Value
& Mask
;
243 if ((Value
& (BIT1
| BIT2
)) == 0) {
247 Length
= ((~Length
) + 1) & 0xffffffff;
249 if ((Value
& BIT3
) == BIT3
) {
259 PcatPciRootBridgeBarExisted (
260 PCI_LIB_ADDRESS (Bus
, Device
, Function
, Offset
),
265 Base
= Base
| LShiftU64 ((UINT64
)OriginalUpperValue
, 32);
266 Length
= Length
| LShiftU64 ((UINT64
)UpperValue
, 32);
268 LowBit
= LowBitSet64 (Length
);
269 Length
= LShiftU64 (1ULL, LowBit
);
272 if ((Value
& BIT3
) == BIT3
) {
273 MemAperture
= PMemAbove4G
;
275 MemAperture
= MemAbove4G
;
279 Limit
= Base
+ Length
- 1;
280 if ((Base
> 0) && (Base
< Limit
)) {
281 if (MemAperture
->Base
> Base
) {
282 MemAperture
->Base
= Base
;
285 if (MemAperture
->Limit
< Limit
) {
286 MemAperture
->Limit
= Limit
;
295 Scan for all root bridges in platform.
297 @param[out] NumberOfRootBridges Number of root bridges detected
299 @retval Pointer to the allocated PCI_ROOT_BRIDGE structure array.
303 OUT UINTN
*NumberOfRootBridges
310 UINTN NumberOfDevices
;
317 PCI_ROOT_BRIDGE_APERTURE Io
;
318 PCI_ROOT_BRIDGE_APERTURE Mem
;
319 PCI_ROOT_BRIDGE_APERTURE MemAbove4G
;
320 PCI_ROOT_BRIDGE_APERTURE PMem
;
321 PCI_ROOT_BRIDGE_APERTURE PMemAbove4G
;
322 PCI_ROOT_BRIDGE_APERTURE
*MemAperture
;
323 PCI_ROOT_BRIDGE
*RootBridges
;
326 *NumberOfRootBridges
= 0;
330 // After scanning all the PCI devices on the PCI root bridge's primary bus,
331 // update the Primary Bus Number for the next PCI root bridge to be this PCI
332 // root bridge's subordinate bus number + 1.
334 for (PrimaryBus
= 0; PrimaryBus
<= PCI_MAX_BUS
; PrimaryBus
= SubBus
+ 1) {
338 ZeroMem (&Io
, sizeof (Io
));
339 ZeroMem (&Mem
, sizeof (Mem
));
340 ZeroMem (&MemAbove4G
, sizeof (MemAbove4G
));
341 ZeroMem (&PMem
, sizeof (PMem
));
342 ZeroMem (&PMemAbove4G
, sizeof (PMemAbove4G
));
343 Io
.Base
= Mem
.Base
= MemAbove4G
.Base
= PMem
.Base
= PMemAbove4G
.Base
= MAX_UINT64
;
345 // Scan all the PCI devices on the primary bus of the PCI root bridge
347 for (Device
= 0, NumberOfDevices
= 0; Device
<= PCI_MAX_DEVICE
; Device
++) {
348 for (Function
= 0; Function
<= PCI_MAX_FUNC
; Function
++) {
350 // Compute the PCI configuration address of the PCI device to probe
352 Address
= PCI_LIB_ADDRESS (PrimaryBus
, Device
, Function
, 0);
355 // Read the Vendor ID from the PCI Configuration Header
357 if (PciRead16 (Address
) == MAX_UINT16
) {
360 // If the PCI Configuration Read fails, or a PCI device does not
361 // exist, then skip this entire PCI device
366 // If PCI function != 0, VendorId == 0xFFFF, we continue to search
374 // Read the entire PCI Configuration Header
376 PciReadBuffer (Address
, sizeof (Pci
), &Pci
);
379 // Increment the number of PCI device found on the primary bus of the
385 // Look for devices with the VGA Palette Snoop enabled in the COMMAND
386 // register of the PCI Config Header
388 if ((Pci
.Hdr
.Command
& EFI_PCI_COMMAND_VGA_PALETTE_SNOOP
) != 0) {
389 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO
;
390 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO_16
;
398 if (IS_PCI_BRIDGE (&Pci
)) {
400 // Get the Bus range that the PPB is decoding
402 if (Pci
.Bridge
.SubordinateBus
> SubBus
) {
404 // If the subordinate bus number of the PCI-PCI bridge is greater
405 // than the PCI root bridge's current subordinate bus number,
406 // then update the PCI root bridge's subordinate bus number
408 SubBus
= Pci
.Bridge
.SubordinateBus
;
412 // Get the I/O range that the PPB is decoding
414 Value
= Pci
.Bridge
.IoBase
& 0x0f;
415 Base
= ((UINT32
)Pci
.Bridge
.IoBase
& 0xf0) << 8;
416 Limit
= (((UINT32
)Pci
.Bridge
.IoLimit
& 0xf0) << 8) | 0x0fff;
418 Base
|= ((UINT32
)Pci
.Bridge
.IoBaseUpper16
<< 16);
419 Limit
|= ((UINT32
)Pci
.Bridge
.IoLimitUpper16
<< 16);
422 if ((Base
> 0) && (Base
< Limit
)) {
423 if (Io
.Base
> Base
) {
427 if (Io
.Limit
< Limit
) {
433 // Get the Memory range that the PPB is decoding
435 Base
= ((UINT32
)Pci
.Bridge
.MemoryBase
& 0xfff0) << 16;
436 Limit
= (((UINT32
)Pci
.Bridge
.MemoryLimit
& 0xfff0) << 16) | 0xfffff;
437 if ((Base
> 0) && (Base
< Limit
)) {
438 if (Mem
.Base
> Base
) {
442 if (Mem
.Limit
< Limit
) {
448 // Get the Prefetchable Memory range that the PPB is decoding
450 Value
= Pci
.Bridge
.PrefetchableMemoryBase
& 0x0f;
451 Base
= ((UINT32
)Pci
.Bridge
.PrefetchableMemoryBase
& 0xfff0) << 16;
452 Limit
= (((UINT32
)Pci
.Bridge
.PrefetchableMemoryLimit
& 0xfff0)
456 Base
|= LShiftU64 (Pci
.Bridge
.PrefetchableBaseUpper32
, 32);
457 Limit
|= LShiftU64 (Pci
.Bridge
.PrefetchableLimitUpper32
, 32);
458 MemAperture
= &PMemAbove4G
;
461 if ((Base
> 0) && (Base
< Limit
)) {
462 if (MemAperture
->Base
> Base
) {
463 MemAperture
->Base
= Base
;
466 if (MemAperture
->Limit
< Limit
) {
467 MemAperture
->Limit
= Limit
;
472 // Look at the PPB Configuration for legacy decoding attributes
474 if ((Pci
.Bridge
.BridgeControl
& EFI_PCI_BRIDGE_CONTROL_ISA
)
475 == EFI_PCI_BRIDGE_CONTROL_ISA
)
477 Attributes
|= EFI_PCI_ATTRIBUTE_ISA_IO
;
478 Attributes
|= EFI_PCI_ATTRIBUTE_ISA_IO_16
;
479 Attributes
|= EFI_PCI_ATTRIBUTE_ISA_MOTHERBOARD_IO
;
482 if ((Pci
.Bridge
.BridgeControl
& EFI_PCI_BRIDGE_CONTROL_VGA
)
483 == EFI_PCI_BRIDGE_CONTROL_VGA
)
485 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO
;
486 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_MEMORY
;
487 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_IO
;
488 if ((Pci
.Bridge
.BridgeControl
& EFI_PCI_BRIDGE_CONTROL_VGA_16
)
491 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO_16
;
492 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_IO_16
;
496 BarOffsetEnd
= OFFSET_OF (PCI_TYPE01
, Bridge
.Bar
[2]);
499 // Parse the BARs of the PCI device to get what I/O Ranges, Memory
500 // Ranges, and Prefetchable Memory Ranges the device is decoding
502 if ((Pci
.Hdr
.HeaderType
& HEADER_LAYOUT_CODE
) == HEADER_TYPE_DEVICE
) {
503 BarOffsetEnd
= OFFSET_OF (PCI_TYPE00
, Device
.Bar
[6]);
507 PcatPciRootBridgeParseBars (
512 OFFSET_OF (PCI_TYPE00
, Device
.Bar
),
522 // See if the PCI device is an IDE controller
526 PCI_CLASS_MASS_STORAGE
,
527 PCI_CLASS_MASS_STORAGE_IDE
530 if (Pci
.Hdr
.ClassCode
[0] & 0x80) {
531 Attributes
|= EFI_PCI_ATTRIBUTE_IDE_PRIMARY_IO
;
532 Attributes
|= EFI_PCI_ATTRIBUTE_IDE_SECONDARY_IO
;
535 if (Pci
.Hdr
.ClassCode
[0] & 0x01) {
536 Attributes
|= EFI_PCI_ATTRIBUTE_IDE_PRIMARY_IO
;
539 if (Pci
.Hdr
.ClassCode
[0] & 0x04) {
540 Attributes
|= EFI_PCI_ATTRIBUTE_IDE_SECONDARY_IO
;
545 // See if the PCI device is a legacy VGA controller or
546 // a standard VGA controller
548 if (IS_CLASS2 (&Pci
, PCI_CLASS_OLD
, PCI_CLASS_OLD_VGA
) ||
549 IS_CLASS2 (&Pci
, PCI_CLASS_DISPLAY
, PCI_CLASS_DISPLAY_VGA
)
552 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO
;
553 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO_16
;
554 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_MEMORY
;
555 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_IO
;
556 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_IO_16
;
560 // See if the PCI Device is a PCI - ISA or PCI - EISA
561 // or ISA_POSITIVE_DECODE Bridge device
563 if (Pci
.Hdr
.ClassCode
[2] == PCI_CLASS_BRIDGE
) {
564 if ((Pci
.Hdr
.ClassCode
[1] == PCI_CLASS_BRIDGE_ISA
) ||
565 (Pci
.Hdr
.ClassCode
[1] == PCI_CLASS_BRIDGE_EISA
) ||
566 (Pci
.Hdr
.ClassCode
[1] == PCI_CLASS_BRIDGE_ISA_PDECODE
))
568 Attributes
|= EFI_PCI_ATTRIBUTE_ISA_IO
;
569 Attributes
|= EFI_PCI_ATTRIBUTE_ISA_IO_16
;
570 Attributes
|= EFI_PCI_ATTRIBUTE_ISA_MOTHERBOARD_IO
;
575 // If this device is not a multi function device, then skip the rest
576 // of this PCI device
578 if ((Function
== 0) && !IS_PCI_MULTI_FUNC (&Pci
)) {
585 // If at least one PCI device was found on the primary bus of this PCI
586 // root bridge, then the PCI root bridge exists.
588 if (NumberOfDevices
> 0) {
589 RootBridges
= ReallocatePool (
590 (*NumberOfRootBridges
) * sizeof (PCI_ROOT_BRIDGE
),
591 (*NumberOfRootBridges
+ 1) * sizeof (PCI_ROOT_BRIDGE
),
594 ASSERT (RootBridges
!= NULL
);
596 AdjustRootBridgeResource (&Io
, &Mem
, &MemAbove4G
, &PMem
, &PMemAbove4G
);
609 &RootBridges
[*NumberOfRootBridges
]
611 RootBridges
[*NumberOfRootBridges
].ResourceAssigned
= TRUE
;
613 // Increment the index for the next PCI Root Bridge
615 (*NumberOfRootBridges
)++;
623 Scan for all root bridges from Universal Payload PciRootBridgeInfoHob
625 @param[in] PciRootBridgeInfo Pointer of Universal Payload PCI Root Bridge Info Hob
626 @param[out] NumberOfRootBridges Number of root bridges detected
628 @retval Pointer to the allocated PCI_ROOT_BRIDGE structure array.
632 RetrieveRootBridgeInfoFromHob (
633 IN UNIVERSAL_PAYLOAD_PCI_ROOT_BRIDGES
*PciRootBridgeInfo
,
634 OUT UINTN
*NumberOfRootBridges
637 PCI_ROOT_BRIDGE
*PciRootBridges
;
641 ASSERT (PciRootBridgeInfo
!= NULL
);
642 ASSERT (NumberOfRootBridges
!= NULL
);
643 if (PciRootBridgeInfo
== NULL
) {
647 if (PciRootBridgeInfo
->Count
== 0) {
651 Size
= PciRootBridgeInfo
->Count
* sizeof (PCI_ROOT_BRIDGE
);
652 PciRootBridges
= (PCI_ROOT_BRIDGE
*)AllocatePool (Size
);
653 ASSERT (PciRootBridges
!= NULL
);
654 if (PciRootBridges
== NULL
) {
658 ZeroMem (PciRootBridges
, PciRootBridgeInfo
->Count
* sizeof (PCI_ROOT_BRIDGE
));
661 // Create all root bridges with PciRootBridgeInfoHob
663 for (Index
= 0; Index
< PciRootBridgeInfo
->Count
; Index
++) {
664 PciRootBridges
[Index
].Segment
= PciRootBridgeInfo
->RootBridge
[Index
].Segment
;
665 PciRootBridges
[Index
].Supports
= PciRootBridgeInfo
->RootBridge
[Index
].Supports
;
666 PciRootBridges
[Index
].Attributes
= PciRootBridgeInfo
->RootBridge
[Index
].Attributes
;
667 PciRootBridges
[Index
].DmaAbove4G
= PciRootBridgeInfo
->RootBridge
[Index
].DmaAbove4G
;
668 PciRootBridges
[Index
].NoExtendedConfigSpace
= PciRootBridgeInfo
->RootBridge
[Index
].NoExtendedConfigSpace
;
669 PciRootBridges
[Index
].ResourceAssigned
= PciRootBridgeInfo
->ResourceAssigned
;
670 PciRootBridges
[Index
].AllocationAttributes
= PciRootBridgeInfo
->RootBridge
[Index
].AllocationAttributes
;
671 PciRootBridges
[Index
].DevicePath
= CreateRootBridgeDevicePath (PciRootBridgeInfo
->RootBridge
[Index
].HID
, PciRootBridgeInfo
->RootBridge
[Index
].UID
);
672 CopyMem (&PciRootBridges
[Index
].Bus
, &PciRootBridgeInfo
->RootBridge
[Index
].Bus
, sizeof (UNIVERSAL_PAYLOAD_PCI_ROOT_BRIDGE_APERTURE
));
673 CopyMem (&PciRootBridges
[Index
].Io
, &PciRootBridgeInfo
->RootBridge
[Index
].Io
, sizeof (UNIVERSAL_PAYLOAD_PCI_ROOT_BRIDGE_APERTURE
));
674 CopyMem (&PciRootBridges
[Index
].Mem
, &PciRootBridgeInfo
->RootBridge
[Index
].Mem
, sizeof (UNIVERSAL_PAYLOAD_PCI_ROOT_BRIDGE_APERTURE
));
675 CopyMem (&PciRootBridges
[Index
].MemAbove4G
, &PciRootBridgeInfo
->RootBridge
[Index
].MemAbove4G
, sizeof (UNIVERSAL_PAYLOAD_PCI_ROOT_BRIDGE_APERTURE
));
676 CopyMem (&PciRootBridges
[Index
].PMem
, &PciRootBridgeInfo
->RootBridge
[Index
].PMem
, sizeof (UNIVERSAL_PAYLOAD_PCI_ROOT_BRIDGE_APERTURE
));
677 CopyMem (&PciRootBridges
[Index
].PMemAbove4G
, &PciRootBridgeInfo
->RootBridge
[Index
].PMemAbove4G
, sizeof (UNIVERSAL_PAYLOAD_PCI_ROOT_BRIDGE_APERTURE
));
680 *NumberOfRootBridges
= PciRootBridgeInfo
->Count
;
683 // Now, this library only supports RootBridge that ResourceAssigned is True
685 if (PciRootBridgeInfo
->ResourceAssigned
) {
686 PcdSetBoolS (PcdPciDisableBusEnumeration
, TRUE
);
688 DEBUG ((DEBUG_ERROR
, "There is root bridge whose ResourceAssigned is FALSE\n"));
689 PcdSetBoolS (PcdPciDisableBusEnumeration
, FALSE
);
693 return PciRootBridges
;