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 This program and the accompanying materials are licensed and made available
7 under the terms and conditions of the BSD License which accompanies this
8 distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
12 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <IndustryStandard/Pci.h>
18 #include <IndustryStandard/Q35MchIch9.h>
20 #include <Protocol/PciHostBridgeResourceAllocation.h>
21 #include <Protocol/PciRootBridgeIo.h>
23 #include <Library/BaseMemoryLib.h>
24 #include <Library/DebugLib.h>
25 #include <Library/MemoryAllocationLib.h>
26 #include <Library/PciHostBridgeLib.h>
27 #include <Library/PciLib.h>
28 #include "PciHostBridge.h"
32 PcatPciRootBridgeBarExisted (
34 OUT UINT32
*OriginalValue
,
39 // Preserve the original value
41 *OriginalValue
= PciRead32 (Address
);
44 // Disable timer interrupt while the BAR is probed
48 PciWrite32 (Address
, 0xFFFFFFFF);
49 *Value
= PciRead32 (Address
);
50 PciWrite32 (Address
, *OriginalValue
);
60 PcatPciRootBridgeParseBars (
65 IN UINTN BarOffsetBase
,
66 IN UINTN BarOffsetEnd
,
67 IN PCI_ROOT_BRIDGE_APERTURE
*Io
,
68 IN PCI_ROOT_BRIDGE_APERTURE
*Mem
,
69 IN PCI_ROOT_BRIDGE_APERTURE
*MemAbove4G
,
70 IN PCI_ROOT_BRIDGE_APERTURE
*PMem
,
71 IN PCI_ROOT_BRIDGE_APERTURE
*PMemAbove4G
77 UINT32 OriginalUpperValue
;
84 PCI_ROOT_BRIDGE_APERTURE
*MemAperture
;
86 for (Offset
= BarOffsetBase
; Offset
< BarOffsetEnd
; Offset
+= sizeof (UINT32
)) {
87 PcatPciRootBridgeBarExisted (
88 PCI_LIB_ADDRESS (Bus
, Device
, Function
, Offset
),
89 &OriginalValue
, &Value
94 if ((Value
& BIT0
) == BIT0
) {
98 if (Command
& EFI_PCI_COMMAND_IO_SPACE
) {
100 Base
= OriginalValue
& Mask
;
101 Length
= ((~(Value
& Mask
)) & Mask
) + 0x04;
102 if (!(Value
& 0xFFFF0000)) {
103 Length
&= 0x0000FFFF;
105 Limit
= Base
+ Length
- 1;
108 if (Io
->Base
> Base
) {
111 if (Io
->Limit
< Limit
) {
120 if (Command
& EFI_PCI_COMMAND_MEMORY_SPACE
) {
123 Base
= OriginalValue
& Mask
;
124 Length
= Value
& Mask
;
126 if ((Value
& (BIT1
| BIT2
)) == 0) {
130 Length
= ((~Length
) + 1) & 0xffffffff;
132 if ((Value
& BIT3
) == BIT3
) {
142 PcatPciRootBridgeBarExisted (
143 PCI_LIB_ADDRESS (Bus
, Device
, Function
, Offset
),
148 Base
= Base
| LShiftU64 ((UINT64
) OriginalUpperValue
, 32);
149 Length
= Length
| LShiftU64 ((UINT64
) UpperValue
, 32);
150 Length
= (~Length
) + 1;
152 if ((Value
& BIT3
) == BIT3
) {
153 MemAperture
= PMemAbove4G
;
155 MemAperture
= MemAbove4G
;
159 Limit
= Base
+ Length
- 1;
161 if (MemAperture
->Base
> Base
) {
162 MemAperture
->Base
= Base
;
164 if (MemAperture
->Limit
< Limit
) {
165 MemAperture
->Limit
= Limit
;
175 UINTN
*NumberOfRootBridges
182 UINTN NumberOfDevices
;
189 PCI_ROOT_BRIDGE_APERTURE Io
, Mem
, MemAbove4G
, PMem
, PMemAbove4G
, *MemAperture
;
190 PCI_ROOT_BRIDGE
*RootBridges
;
194 *NumberOfRootBridges
= 0;
198 // After scanning all the PCI devices on the PCI root bridge's primary bus,
199 // update the Primary Bus Number for the next PCI root bridge to be this PCI
200 // root bridge's subordinate bus number + 1.
202 for (PrimaryBus
= 0; PrimaryBus
<= PCI_MAX_BUS
; PrimaryBus
= SubBus
+ 1) {
205 Io
.Base
= Mem
.Base
= MemAbove4G
.Base
= PMem
.Base
= PMemAbove4G
.Base
= MAX_UINT64
;
206 Io
.Limit
= Mem
.Limit
= MemAbove4G
.Limit
= PMem
.Limit
= PMemAbove4G
.Limit
= 0;
208 // Scan all the PCI devices on the primary bus of the PCI root bridge
210 for (Device
= 0, NumberOfDevices
= 0; Device
<= PCI_MAX_DEVICE
; Device
++) {
212 for (Function
= 0; Function
<= PCI_MAX_FUNC
; Function
++) {
215 // Compute the PCI configuration address of the PCI device to probe
217 Address
= PCI_LIB_ADDRESS (PrimaryBus
, Device
, Function
, 0);
220 // Read the Vendor ID from the PCI Configuration Header
222 if (PciRead16 (Address
) == MAX_UINT16
) {
225 // If the PCI Configuration Read fails, or a PCI device does not
226 // exist, then skip this entire PCI device
231 // If PCI function != 0, VendorId == 0xFFFF, we continue to search
239 // Read the entire PCI Configuration Header
241 PciReadBuffer (Address
, sizeof (Pci
), &Pci
);
244 // Increment the number of PCI device found on the primary bus of the
250 // Look for devices with the VGA Palette Snoop enabled in the COMMAND
251 // register of the PCI Config Header
253 if ((Pci
.Hdr
.Command
& EFI_PCI_COMMAND_VGA_PALETTE_SNOOP
) != 0) {
254 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO
;
255 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO_16
;
263 if (IS_PCI_BRIDGE (&Pci
)) {
265 // Get the Bus range that the PPB is decoding
267 if (Pci
.Bridge
.SubordinateBus
> SubBus
) {
269 // If the suborinate bus number of the PCI-PCI bridge is greater
270 // than the PCI root bridge's current subordinate bus number,
271 // then update the PCI root bridge's subordinate bus number
273 SubBus
= Pci
.Bridge
.SubordinateBus
;
277 // Get the I/O range that the PPB is decoding
279 Value
= Pci
.Bridge
.IoBase
& 0x0f;
280 Base
= ((UINT32
) Pci
.Bridge
.IoBase
& 0xf0) << 8;
281 Limit
= (((UINT32
) Pci
.Bridge
.IoLimit
& 0xf0) << 8) | 0x0fff;
283 Base
|= ((UINT32
) Pci
.Bridge
.IoBaseUpper16
<< 16);
284 Limit
|= ((UINT32
) Pci
.Bridge
.IoLimitUpper16
<< 16);
287 if (Io
.Base
> Base
) {
290 if (Io
.Limit
< Limit
) {
296 // Get the Memory range that the PPB is decoding
298 Base
= ((UINT32
) Pci
.Bridge
.MemoryBase
& 0xfff0) << 16;
299 Limit
= (((UINT32
) Pci
.Bridge
.MemoryLimit
& 0xfff0) << 16) | 0xfffff;
301 if (Mem
.Base
> Base
) {
304 if (Mem
.Limit
< Limit
) {
310 // Get the Prefetchable Memory range that the PPB is decoding
312 Value
= Pci
.Bridge
.PrefetchableMemoryBase
& 0x0f;
313 Base
= ((UINT32
) Pci
.Bridge
.PrefetchableMemoryBase
& 0xfff0) << 16;
314 Limit
= (((UINT32
) Pci
.Bridge
.PrefetchableMemoryLimit
& 0xfff0)
318 Base
|= LShiftU64 (Pci
.Bridge
.PrefetchableBaseUpper32
, 32);
319 Limit
|= LShiftU64 (Pci
.Bridge
.PrefetchableLimitUpper32
, 32);
320 MemAperture
= &PMemAbove4G
;
323 if (MemAperture
->Base
> Base
) {
324 MemAperture
->Base
= Base
;
326 if (MemAperture
->Limit
< Limit
) {
327 MemAperture
->Limit
= Limit
;
332 // Look at the PPB Configuration for legacy decoding attributes
334 if ((Pci
.Bridge
.BridgeControl
& EFI_PCI_BRIDGE_CONTROL_ISA
)
335 == EFI_PCI_BRIDGE_CONTROL_ISA
) {
336 Attributes
|= EFI_PCI_ATTRIBUTE_ISA_IO
;
337 Attributes
|= EFI_PCI_ATTRIBUTE_ISA_IO_16
;
338 Attributes
|= EFI_PCI_ATTRIBUTE_ISA_MOTHERBOARD_IO
;
340 if ((Pci
.Bridge
.BridgeControl
& EFI_PCI_BRIDGE_CONTROL_VGA
)
341 == EFI_PCI_BRIDGE_CONTROL_VGA
) {
342 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO
;
343 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_MEMORY
;
344 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_IO
;
345 if ((Pci
.Bridge
.BridgeControl
& EFI_PCI_BRIDGE_CONTROL_VGA_16
)
347 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO_16
;
348 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_IO_16
;
352 BarOffsetEnd
= OFFSET_OF (PCI_TYPE01
, Bridge
.Bar
[2]);
355 // Parse the BARs of the PCI device to get what I/O Ranges, Memory
356 // Ranges, and Prefetchable Memory Ranges the device is decoding
358 if ((Pci
.Hdr
.HeaderType
& HEADER_LAYOUT_CODE
) == HEADER_TYPE_DEVICE
) {
359 BarOffsetEnd
= OFFSET_OF (PCI_TYPE00
, Device
.Bar
[6]);
363 PcatPciRootBridgeParseBars (
368 OFFSET_OF (PCI_TYPE00
, Device
.Bar
),
376 // See if the PCI device is an IDE controller
378 if (IS_CLASS2 (&Pci
, PCI_CLASS_MASS_STORAGE
,
379 PCI_CLASS_MASS_STORAGE_IDE
)) {
380 if (Pci
.Hdr
.ClassCode
[0] & 0x80) {
381 Attributes
|= EFI_PCI_ATTRIBUTE_IDE_PRIMARY_IO
;
382 Attributes
|= EFI_PCI_ATTRIBUTE_IDE_SECONDARY_IO
;
384 if (Pci
.Hdr
.ClassCode
[0] & 0x01) {
385 Attributes
|= EFI_PCI_ATTRIBUTE_IDE_PRIMARY_IO
;
387 if (Pci
.Hdr
.ClassCode
[0] & 0x04) {
388 Attributes
|= EFI_PCI_ATTRIBUTE_IDE_SECONDARY_IO
;
393 // See if the PCI device is a legacy VGA controller or
394 // a standard VGA controller
396 if (IS_CLASS2 (&Pci
, PCI_CLASS_OLD
, PCI_CLASS_OLD_VGA
) ||
397 IS_CLASS2 (&Pci
, PCI_CLASS_DISPLAY
, PCI_CLASS_DISPLAY_VGA
)
399 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO
;
400 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO_16
;
401 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_MEMORY
;
402 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_IO
;
403 Attributes
|= EFI_PCI_ATTRIBUTE_VGA_IO_16
;
407 // See if the PCI Device is a PCI - ISA or PCI - EISA
408 // or ISA_POSITIVIE_DECODE Bridge device
410 if (Pci
.Hdr
.ClassCode
[2] == PCI_CLASS_BRIDGE
) {
411 if (Pci
.Hdr
.ClassCode
[1] == PCI_CLASS_BRIDGE_ISA
||
412 Pci
.Hdr
.ClassCode
[1] == PCI_CLASS_BRIDGE_EISA
||
413 Pci
.Hdr
.ClassCode
[1] == PCI_CLASS_BRIDGE_ISA_PDECODE
) {
414 Attributes
|= EFI_PCI_ATTRIBUTE_ISA_IO
;
415 Attributes
|= EFI_PCI_ATTRIBUTE_ISA_IO_16
;
416 Attributes
|= EFI_PCI_ATTRIBUTE_ISA_MOTHERBOARD_IO
;
421 // If this device is not a multi function device, then skip the rest
422 // of this PCI device
424 if (Function
== 0 && !IS_PCI_MULTI_FUNC (&Pci
)) {
431 // If at least one PCI device was found on the primary bus of this PCI
432 // root bridge, then the PCI root bridge exists.
434 if (NumberOfDevices
> 0) {
435 RootBridges
= ReallocatePool (
436 (*NumberOfRootBridges
) * sizeof (PCI_ROOT_BRIDGE
),
437 (*NumberOfRootBridges
+ 1) * sizeof (PCI_ROOT_BRIDGE
),
440 ASSERT (RootBridges
!= NULL
);
442 Attributes
, Attributes
, 0,
443 (UINT8
) PrimaryBus
, (UINT8
) SubBus
,
444 &Io
, &Mem
, &MemAbove4G
, &PMem
, &PMemAbove4G
,
445 &RootBridges
[*NumberOfRootBridges
]
447 RootBridges
[*NumberOfRootBridges
].ResourceAssigned
= TRUE
;
449 // Increment the index for the next PCI Root Bridge
451 (*NumberOfRootBridges
)++;