2 PCI emumeration support functions implementation for PCI Bus module.
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this 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,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 extern CHAR16
*mBarTypeStr
[];
20 #define OLD_ALIGN 0xFFFFFFFFFFFFFFFFULL
21 #define EVEN_ALIGN 0xFFFFFFFFFFFFFFFEULL
22 #define SQUAD_ALIGN 0xFFFFFFFFFFFFFFFDULL
23 #define DQUAD_ALIGN 0xFFFFFFFFFFFFFFFCULL
26 This routine is used to check whether the pci device is present.
28 @param PciRootBridgeIo Pointer to instance of EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
29 @param Pci Output buffer for PCI device configuration space.
30 @param Bus PCI bus NO.
31 @param Device PCI device NO.
32 @param Func PCI Func NO.
34 @retval EFI_NOT_FOUND PCI device not present.
35 @retval EFI_SUCCESS PCI device is found.
40 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
,
51 // Create PCI address map in terms of Bus, Device and Func
53 Address
= EFI_PCI_ADDRESS (Bus
, Device
, Func
, 0);
56 // Read the Vendor ID register
58 Status
= PciRootBridgeIo
->Pci
.Read (
66 if (!EFI_ERROR (Status
) && (Pci
->Hdr
).VendorId
!= 0xffff) {
68 // Read the entire config header for the device
70 Status
= PciRootBridgeIo
->Pci
.Read (
74 sizeof (PCI_TYPE00
) / sizeof (UINT32
),
85 Collect all the resource information under this root bridge.
87 A database that records all the information about pci device subject to this
88 root bridge will then be created.
90 @param Bridge Parent bridge instance.
91 @param StartBusNumber Bus number of begining.
93 @retval EFI_SUCCESS PCI device is found.
94 @retval other Some error occurred when reading PCI bridge information.
98 PciPciDeviceInfoCollector (
99 IN PCI_IO_DEVICE
*Bridge
,
100 IN UINT8 StartBusNumber
108 PCI_IO_DEVICE
*PciIoDevice
;
109 EFI_PCI_IO_PROTOCOL
*PciIo
;
111 Status
= EFI_SUCCESS
;
114 for (Device
= 0; Device
<= PCI_MAX_DEVICE
; Device
++) {
116 for (Func
= 0; Func
<= PCI_MAX_FUNC
; Func
++) {
119 // Check to see whether PCI device is present
121 Status
= PciDevicePresent (
122 Bridge
->PciRootBridgeIo
,
124 (UINT8
) StartBusNumber
,
129 if (EFI_ERROR (Status
) && Func
== 0) {
131 // go to next device if there is no Function 0
136 if (!EFI_ERROR (Status
)) {
139 // Call back to host bridge function
141 PreprocessController (Bridge
, (UINT8
) StartBusNumber
, Device
, Func
, EfiPciBeforeResourceCollection
);
144 // Collect all the information about the PCI device discovered
146 Status
= PciSearchDevice (
149 (UINT8
) StartBusNumber
,
156 // Recursively scan PCI busses on the other side of PCI-PCI bridges
159 if (!EFI_ERROR (Status
) && (IS_PCI_BRIDGE (&Pci
) || IS_CARDBUS_BRIDGE (&Pci
))) {
162 // If it is PPB, we need to get the secondary bus to continue the enumeration
164 PciIo
= &(PciIoDevice
->PciIo
);
166 Status
= PciIo
->Pci
.Read (PciIo
, EfiPciIoWidthUint8
, PCI_BRIDGE_SECONDARY_BUS_REGISTER_OFFSET
, 1, &SecBus
);
168 if (EFI_ERROR (Status
)) {
173 // Ensure secondary bus number is greater than the primary bus number to avoid
174 // any potential dead loop when PcdPciDisableBusEnumeration is set to TRUE
176 if (SecBus
<= StartBusNumber
) {
181 // Get resource padding for PPB
183 GetResourcePaddingPpb (PciIoDevice
);
186 // Deep enumerate the next level bus
188 Status
= PciPciDeviceInfoCollector (
195 if (Func
== 0 && !IS_PCI_MULTI_FUNC (&Pci
)) {
198 // Skip sub functions, this is not a multi function device
211 Seach required device and create PCI device instance.
213 @param Bridge Parent bridge instance.
214 @param Pci Input PCI device information block.
215 @param Bus PCI bus NO.
216 @param Device PCI device NO.
217 @param Func PCI func NO.
218 @param PciDevice Output of searched PCI device instance.
220 @retval EFI_SUCCESS Successfully created PCI device instance.
221 @retval EFI_OUT_OF_RESOURCES Cannot get PCI device information.
226 IN PCI_IO_DEVICE
*Bridge
,
231 OUT PCI_IO_DEVICE
**PciDevice
234 PCI_IO_DEVICE
*PciIoDevice
;
240 "PciBus: Discovered %s @ [%02x|%02x|%02x]\n",
241 IS_PCI_BRIDGE (Pci
) ? L
"PPB" :
242 IS_CARDBUS_BRIDGE (Pci
) ? L
"P2C" :
247 if (!IS_PCI_BRIDGE (Pci
)) {
249 if (IS_CARDBUS_BRIDGE (Pci
)) {
250 PciIoDevice
= GatherP2CInfo (
257 if ((PciIoDevice
!= NULL
) && gFullEnumeration
) {
258 InitializeP2C (PciIoDevice
);
263 // Create private data for Pci Device
265 PciIoDevice
= GatherDeviceInfo (
278 // Create private data for PPB
280 PciIoDevice
= GatherPpbInfo (
289 // Special initialization for PPB including making the PPB quiet
291 if ((PciIoDevice
!= NULL
) && gFullEnumeration
) {
292 InitializePpb (PciIoDevice
);
296 if (PciIoDevice
== NULL
) {
297 return EFI_OUT_OF_RESOURCES
;
301 // Update the bar information for this PCI device so as to support some specific device
303 UpdatePciInfo (PciIoDevice
);
305 if (PciIoDevice
->DevicePath
== NULL
) {
306 return EFI_OUT_OF_RESOURCES
;
310 // Detect this function has option rom
312 if (gFullEnumeration
) {
314 if (!IS_CARDBUS_BRIDGE (Pci
)) {
316 GetOpRomInfo (PciIoDevice
);
320 ResetPowerManagementFeature (PciIoDevice
);
325 // Insert it into a global tree for future reference
327 InsertPciDevice (Bridge
, PciIoDevice
);
330 // Determine PCI device attributes
333 if (PciDevice
!= NULL
) {
334 *PciDevice
= PciIoDevice
;
341 Dump the PPB padding resource information.
343 @param PciIoDevice PCI IO instance.
344 @param ResourceType The desired resource type to dump.
345 PciBarTypeUnknown means to dump all types of resources.
348 DumpPpbPaddingResource (
349 IN PCI_IO_DEVICE
*PciIoDevice
,
350 IN PCI_BAR_TYPE ResourceType
353 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Descriptor
;
356 if (PciIoDevice
->ResourcePaddingDescriptors
== NULL
) {
360 if (ResourceType
== PciBarTypeIo16
|| ResourceType
== PciBarTypeIo32
) {
361 ResourceType
= PciBarTypeIo
;
364 for (Descriptor
= PciIoDevice
->ResourcePaddingDescriptors
; Descriptor
->Desc
!= ACPI_END_TAG_DESCRIPTOR
; Descriptor
++) {
366 Type
= PciBarTypeUnknown
;
367 if (Descriptor
->Desc
== ACPI_ADDRESS_SPACE_DESCRIPTOR
&& Descriptor
->ResType
== ACPI_ADDRESS_SPACE_TYPE_IO
) {
369 } else if (Descriptor
->Desc
== ACPI_ADDRESS_SPACE_DESCRIPTOR
&& Descriptor
->ResType
== ACPI_ADDRESS_SPACE_TYPE_MEM
) {
371 if (Descriptor
->AddrSpaceGranularity
== 32) {
375 if (Descriptor
->SpecificFlag
== EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE
) {
376 Type
= PciBarTypePMem32
;
382 if (Descriptor
->SpecificFlag
== 0) {
383 Type
= PciBarTypeMem32
;
387 if (Descriptor
->AddrSpaceGranularity
== 64) {
391 if (Descriptor
->SpecificFlag
== EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE
) {
392 Type
= PciBarTypePMem64
;
398 if (Descriptor
->SpecificFlag
== 0) {
399 Type
= PciBarTypeMem64
;
404 if ((Type
!= PciBarTypeUnknown
) && ((ResourceType
== PciBarTypeUnknown
) || (ResourceType
== Type
))) {
407 " Padding: Type = %s; Alignment = 0x%lx;\tLength = 0x%lx\n",
408 mBarTypeStr
[Type
], Descriptor
->AddrRangeMax
, Descriptor
->AddrLen
416 Dump the PCI BAR information.
418 @param PciIoDevice PCI IO instance.
422 IN PCI_IO_DEVICE
*PciIoDevice
427 for (Index
= 0; Index
< PCI_MAX_BAR
; Index
++) {
428 if (PciIoDevice
->PciBar
[Index
].BarType
== PciBarTypeUnknown
) {
434 " BAR[%d]: Type = %s; Alignment = 0x%lx;\tLength = 0x%lx;\tOffset = 0x%02x\n",
435 Index
, mBarTypeStr
[MIN (PciIoDevice
->PciBar
[Index
].BarType
, PciBarTypeMaxType
)],
436 PciIoDevice
->PciBar
[Index
].Alignment
, PciIoDevice
->PciBar
[Index
].Length
, PciIoDevice
->PciBar
[Index
].Offset
440 for (Index
= 0; Index
< PCI_MAX_BAR
; Index
++) {
441 if ((PciIoDevice
->VfPciBar
[Index
].BarType
== PciBarTypeUnknown
) && (PciIoDevice
->VfPciBar
[Index
].Length
== 0)) {
447 " VFBAR[%d]: Type = %s; Alignment = 0x%lx;\tLength = 0x%lx;\tOffset = 0x%02x\n",
448 Index
, mBarTypeStr
[MIN (PciIoDevice
->VfPciBar
[Index
].BarType
, PciBarTypeMaxType
)],
449 PciIoDevice
->VfPciBar
[Index
].Alignment
, PciIoDevice
->VfPciBar
[Index
].Length
, PciIoDevice
->VfPciBar
[Index
].Offset
452 DEBUG ((EFI_D_INFO
, "\n"));
456 Create PCI device instance for PCI device.
458 @param Bridge Parent bridge instance.
459 @param Pci Input PCI device information block.
460 @param Bus PCI device Bus NO.
461 @param Device PCI device Device NO.
462 @param Func PCI device's func NO.
464 @return Created PCI device instance.
469 IN PCI_IO_DEVICE
*Bridge
,
478 PCI_IO_DEVICE
*PciIoDevice
;
480 PciIoDevice
= CreatePciIoDevice (
488 if (PciIoDevice
== NULL
) {
493 // If it is a full enumeration, disconnect the device in advance
495 if (gFullEnumeration
) {
497 PCI_DISABLE_COMMAND_REGISTER (PciIoDevice
, EFI_PCI_COMMAND_BITS_OWNED
);
502 // Start to parse the bars
504 for (Offset
= 0x10, BarIndex
= 0; Offset
<= 0x24 && BarIndex
< PCI_MAX_BAR
; BarIndex
++) {
505 Offset
= PciParseBar (PciIoDevice
, Offset
, BarIndex
);
509 // Parse the SR-IOV VF bars
511 if (PcdGetBool (PcdSrIovSupport
) && PciIoDevice
->SrIovCapabilityOffset
!= 0) {
512 for (Offset
= PciIoDevice
->SrIovCapabilityOffset
+ EFI_PCIE_CAPABILITY_ID_SRIOV_BAR0
, BarIndex
= 0;
513 Offset
<= PciIoDevice
->SrIovCapabilityOffset
+ EFI_PCIE_CAPABILITY_ID_SRIOV_BAR5
;
516 ASSERT (BarIndex
< PCI_MAX_BAR
);
517 Offset
= PciIovParseVfBar (PciIoDevice
, Offset
, BarIndex
);
521 DEBUG_CODE (DumpPciBars (PciIoDevice
););
526 Create PCI device instance for PCI-PCI bridge.
528 @param Bridge Parent bridge instance.
529 @param Pci Input PCI device information block.
530 @param Bus PCI device Bus NO.
531 @param Device PCI device Device NO.
532 @param Func PCI device's func NO.
534 @return Created PCI device instance.
539 IN PCI_IO_DEVICE
*Bridge
,
546 PCI_IO_DEVICE
*PciIoDevice
;
549 EFI_PCI_IO_PROTOCOL
*PciIo
;
551 UINT32 PMemBaseLimit
;
552 UINT16 PrefetchableMemoryBase
;
553 UINT16 PrefetchableMemoryLimit
;
555 PciIoDevice
= CreatePciIoDevice (
563 if (PciIoDevice
== NULL
) {
567 if (gFullEnumeration
) {
568 PCI_DISABLE_COMMAND_REGISTER (PciIoDevice
, EFI_PCI_COMMAND_BITS_OWNED
);
571 // Initalize the bridge control register
573 PCI_DISABLE_BRIDGE_CONTROL_REGISTER (PciIoDevice
, EFI_PCI_BRIDGE_CONTROL_BITS_OWNED
);
578 // PPB can have two BARs
580 if (PciParseBar (PciIoDevice
, 0x10, PPB_BAR_0
) == 0x14) {
584 PciParseBar (PciIoDevice
, 0x14, PPB_BAR_1
);
587 PciIo
= &PciIoDevice
->PciIo
;
590 // Test whether it support 32 decode or not
592 PciIo
->Pci
.Read (PciIo
, EfiPciIoWidthUint8
, 0x1C, 1, &Temp
);
593 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint8
, 0x1C, 1, &gAllOne
);
594 PciIo
->Pci
.Read (PciIo
, EfiPciIoWidthUint8
, 0x1C, 1, &Value
);
595 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint8
, 0x1C, 1, &Temp
);
598 if ((Value
& 0x01) != 0) {
599 PciIoDevice
->Decodes
|= EFI_BRIDGE_IO32_DECODE_SUPPORTED
;
601 PciIoDevice
->Decodes
|= EFI_BRIDGE_IO16_DECODE_SUPPORTED
;
606 // if PcdPciBridgeIoAlignmentProbe is TRUE, PCI bus driver probes
607 // PCI bridge supporting non-standard I/O window alignment less than 4K.
610 PciIoDevice
->BridgeIoAlignment
= 0xFFF;
611 if (FeaturePcdGet (PcdPciBridgeIoAlignmentProbe
)) {
613 // Check any bits of bit 3-1 of I/O Base Register are writable.
614 // if so, it is assumed non-standard I/O window alignment is supported by this bridge.
615 // Per spec, bit 3-1 of I/O Base Register are reserved bits, so its content can't be assumed.
617 Value
= (UINT8
)(Temp
^ (BIT3
| BIT2
| BIT1
));
618 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint8
, 0x1C, 1, &Value
);
619 PciIo
->Pci
.Read (PciIo
, EfiPciIoWidthUint8
, 0x1C, 1, &Value
);
620 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint8
, 0x1C, 1, &Temp
);
621 Value
= (UINT8
)((Value
^ Temp
) & (BIT3
| BIT2
| BIT1
));
624 PciIoDevice
->BridgeIoAlignment
= 0x7FF;
627 PciIoDevice
->BridgeIoAlignment
= 0x3FF;
629 case BIT3
| BIT2
| BIT1
:
630 PciIoDevice
->BridgeIoAlignment
= 0x1FF;
635 Status
= BarExisted (
643 // Test if it supports 64 memory or not
645 // The bottom 4 bits of both the Prefetchable Memory Base and Prefetchable Memory Limit
647 // 0 - the bridge supports only 32 bit addresses.
648 // 1 - the bridge supports 64-bit addresses.
650 PrefetchableMemoryBase
= (UINT16
)(PMemBaseLimit
& 0xffff);
651 PrefetchableMemoryLimit
= (UINT16
)(PMemBaseLimit
>> 16);
652 if (!EFI_ERROR (Status
) &&
653 (PrefetchableMemoryBase
& 0x000f) == 0x0001 &&
654 (PrefetchableMemoryLimit
& 0x000f) == 0x0001) {
655 Status
= BarExisted (
662 if (!EFI_ERROR (Status
)) {
663 PciIoDevice
->Decodes
|= EFI_BRIDGE_PMEM32_DECODE_SUPPORTED
;
664 PciIoDevice
->Decodes
|= EFI_BRIDGE_PMEM64_DECODE_SUPPORTED
;
666 PciIoDevice
->Decodes
|= EFI_BRIDGE_PMEM32_DECODE_SUPPORTED
;
671 // Memory 32 code is required for ppb
673 PciIoDevice
->Decodes
|= EFI_BRIDGE_MEM32_DECODE_SUPPORTED
;
675 GetResourcePaddingPpb (PciIoDevice
);
678 DumpPpbPaddingResource (PciIoDevice
, PciBarTypeUnknown
);
679 DumpPciBars (PciIoDevice
);
687 Create PCI device instance for PCI Card bridge device.
689 @param Bridge Parent bridge instance.
690 @param Pci Input PCI device information block.
691 @param Bus PCI device Bus NO.
692 @param Device PCI device Device NO.
693 @param Func PCI device's func NO.
695 @return Created PCI device instance.
700 IN PCI_IO_DEVICE
*Bridge
,
707 PCI_IO_DEVICE
*PciIoDevice
;
709 PciIoDevice
= CreatePciIoDevice (
717 if (PciIoDevice
== NULL
) {
721 if (gFullEnumeration
) {
722 PCI_DISABLE_COMMAND_REGISTER (PciIoDevice
, EFI_PCI_COMMAND_BITS_OWNED
);
725 // Initalize the bridge control register
727 PCI_DISABLE_BRIDGE_CONTROL_REGISTER (PciIoDevice
, EFI_PCCARD_BRIDGE_CONTROL_BITS_OWNED
);
731 // P2C only has one bar that is in 0x10
733 PciParseBar (PciIoDevice
, 0x10, P2C_BAR_0
);
736 // Read PciBar information from the bar register
738 GetBackPcCardBar (PciIoDevice
);
739 PciIoDevice
->Decodes
= EFI_BRIDGE_MEM32_DECODE_SUPPORTED
|
740 EFI_BRIDGE_PMEM32_DECODE_SUPPORTED
|
741 EFI_BRIDGE_IO32_DECODE_SUPPORTED
;
743 DEBUG_CODE (DumpPciBars (PciIoDevice
););
749 Create device path for pci deivce.
751 @param ParentDevicePath Parent bridge's path.
752 @param PciIoDevice Pci device instance.
754 @return Device path protocol instance for specific pci device.
757 EFI_DEVICE_PATH_PROTOCOL
*
758 CreatePciDevicePath (
759 IN EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
,
760 IN PCI_IO_DEVICE
*PciIoDevice
764 PCI_DEVICE_PATH PciNode
;
767 // Create PCI device path
769 PciNode
.Header
.Type
= HARDWARE_DEVICE_PATH
;
770 PciNode
.Header
.SubType
= HW_PCI_DP
;
771 SetDevicePathNodeLength (&PciNode
.Header
, sizeof (PciNode
));
773 PciNode
.Device
= PciIoDevice
->DeviceNumber
;
774 PciNode
.Function
= PciIoDevice
->FunctionNumber
;
775 PciIoDevice
->DevicePath
= AppendDevicePathNode (ParentDevicePath
, &PciNode
.Header
);
777 return PciIoDevice
->DevicePath
;
781 Check whether the PCI IOV VF bar is existed or not.
783 @param PciIoDevice A pointer to the PCI_IO_DEVICE.
784 @param Offset The offset.
785 @param BarLengthValue The bar length value returned.
786 @param OriginalBarValue The original bar value returned.
788 @retval EFI_NOT_FOUND The bar doesn't exist.
789 @retval EFI_SUCCESS The bar exist.
794 IN PCI_IO_DEVICE
*PciIoDevice
,
796 OUT UINT32
*BarLengthValue
,
797 OUT UINT32
*OriginalBarValue
800 EFI_PCI_IO_PROTOCOL
*PciIo
;
801 UINT32 OriginalValue
;
806 // Ensure it is called properly
808 ASSERT (PciIoDevice
->SrIovCapabilityOffset
!= 0);
809 if (PciIoDevice
->SrIovCapabilityOffset
== 0) {
810 return EFI_NOT_FOUND
;
813 PciIo
= &PciIoDevice
->PciIo
;
816 // Preserve the original value
819 PciIo
->Pci
.Read (PciIo
, EfiPciIoWidthUint32
, (UINT32
)Offset
, 1, &OriginalValue
);
822 // Raise TPL to high level to disable timer interrupt while the BAR is probed
824 OldTpl
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
826 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint32
, (UINT32
)Offset
, 1, &gAllOne
);
827 PciIo
->Pci
.Read (PciIo
, EfiPciIoWidthUint32
, (UINT32
)Offset
, 1, &Value
);
830 // Write back the original value
832 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint32
, (UINT32
)Offset
, 1, &OriginalValue
);
835 // Restore TPL to its original level
837 gBS
->RestoreTPL (OldTpl
);
839 if (BarLengthValue
!= NULL
) {
840 *BarLengthValue
= Value
;
843 if (OriginalBarValue
!= NULL
) {
844 *OriginalBarValue
= OriginalValue
;
848 return EFI_NOT_FOUND
;
855 Check whether the bar is existed or not.
857 @param PciIoDevice A pointer to the PCI_IO_DEVICE.
858 @param Offset The offset.
859 @param BarLengthValue The bar length value returned.
860 @param OriginalBarValue The original bar value returned.
862 @retval EFI_NOT_FOUND The bar doesn't exist.
863 @retval EFI_SUCCESS The bar exist.
868 IN PCI_IO_DEVICE
*PciIoDevice
,
870 OUT UINT32
*BarLengthValue
,
871 OUT UINT32
*OriginalBarValue
874 EFI_PCI_IO_PROTOCOL
*PciIo
;
875 UINT32 OriginalValue
;
879 PciIo
= &PciIoDevice
->PciIo
;
882 // Preserve the original value
884 PciIo
->Pci
.Read (PciIo
, EfiPciIoWidthUint32
, (UINT8
) Offset
, 1, &OriginalValue
);
887 // Raise TPL to high level to disable timer interrupt while the BAR is probed
889 OldTpl
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
891 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint32
, (UINT8
) Offset
, 1, &gAllOne
);
892 PciIo
->Pci
.Read (PciIo
, EfiPciIoWidthUint32
, (UINT8
) Offset
, 1, &Value
);
895 // Write back the original value
897 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint32
, (UINT8
) Offset
, 1, &OriginalValue
);
900 // Restore TPL to its original level
902 gBS
->RestoreTPL (OldTpl
);
904 if (BarLengthValue
!= NULL
) {
905 *BarLengthValue
= Value
;
908 if (OriginalBarValue
!= NULL
) {
909 *OriginalBarValue
= OriginalValue
;
913 return EFI_NOT_FOUND
;
920 Test whether the device can support given attributes.
922 @param PciIoDevice Pci device instance.
923 @param Command Input command register value, and
924 returned supported register value.
925 @param BridgeControl Inout bridge control value for PPB or P2C, and
926 returned supported bridge control value.
927 @param OldCommand Returned and stored old command register offset.
928 @param OldBridgeControl Returned and stored old Bridge control value for PPB or P2C.
932 PciTestSupportedAttribute (
933 IN PCI_IO_DEVICE
*PciIoDevice
,
934 IN OUT UINT16
*Command
,
935 IN OUT UINT16
*BridgeControl
,
936 OUT UINT16
*OldCommand
,
937 OUT UINT16
*OldBridgeControl
943 // Preserve the original value
945 PCI_READ_COMMAND_REGISTER (PciIoDevice
, OldCommand
);
948 // Raise TPL to high level to disable timer interrupt while the BAR is probed
950 OldTpl
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
952 PCI_SET_COMMAND_REGISTER (PciIoDevice
, *Command
);
953 PCI_READ_COMMAND_REGISTER (PciIoDevice
, Command
);
956 // Write back the original value
958 PCI_SET_COMMAND_REGISTER (PciIoDevice
, *OldCommand
);
961 // Restore TPL to its original level
963 gBS
->RestoreTPL (OldTpl
);
965 if (IS_PCI_BRIDGE (&PciIoDevice
->Pci
) || IS_CARDBUS_BRIDGE (&PciIoDevice
->Pci
)) {
968 // Preserve the original value
970 PCI_READ_BRIDGE_CONTROL_REGISTER (PciIoDevice
, OldBridgeControl
);
973 // Raise TPL to high level to disable timer interrupt while the BAR is probed
975 OldTpl
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
977 PCI_SET_BRIDGE_CONTROL_REGISTER (PciIoDevice
, *BridgeControl
);
978 PCI_READ_BRIDGE_CONTROL_REGISTER (PciIoDevice
, BridgeControl
);
981 // Write back the original value
983 PCI_SET_BRIDGE_CONTROL_REGISTER (PciIoDevice
, *OldBridgeControl
);
986 // Restore TPL to its original level
988 gBS
->RestoreTPL (OldTpl
);
991 *OldBridgeControl
= 0;
997 Set the supported or current attributes of a PCI device.
999 @param PciIoDevice Structure pointer for PCI device.
1000 @param Command Command register value.
1001 @param BridgeControl Bridge control value for PPB or P2C.
1002 @param Option Make a choice of EFI_SET_SUPPORTS or EFI_SET_ATTRIBUTES.
1006 PciSetDeviceAttribute (
1007 IN PCI_IO_DEVICE
*PciIoDevice
,
1009 IN UINT16 BridgeControl
,
1017 if ((Command
& EFI_PCI_COMMAND_IO_SPACE
) != 0) {
1018 Attributes
|= EFI_PCI_IO_ATTRIBUTE_IO
;
1021 if ((Command
& EFI_PCI_COMMAND_MEMORY_SPACE
) != 0) {
1022 Attributes
|= EFI_PCI_IO_ATTRIBUTE_MEMORY
;
1025 if ((Command
& EFI_PCI_COMMAND_BUS_MASTER
) != 0) {
1026 Attributes
|= EFI_PCI_IO_ATTRIBUTE_BUS_MASTER
;
1029 if ((Command
& EFI_PCI_COMMAND_VGA_PALETTE_SNOOP
) != 0) {
1030 Attributes
|= EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO
;
1033 if ((BridgeControl
& EFI_PCI_BRIDGE_CONTROL_ISA
) != 0) {
1034 Attributes
|= EFI_PCI_IO_ATTRIBUTE_ISA_IO
;
1037 if ((BridgeControl
& EFI_PCI_BRIDGE_CONTROL_VGA
) != 0) {
1038 Attributes
|= EFI_PCI_IO_ATTRIBUTE_VGA_IO
;
1039 Attributes
|= EFI_PCI_IO_ATTRIBUTE_VGA_MEMORY
;
1040 Attributes
|= EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO
;
1043 if ((BridgeControl
& EFI_PCI_BRIDGE_CONTROL_VGA_16
) != 0) {
1044 Attributes
|= EFI_PCI_IO_ATTRIBUTE_VGA_IO_16
;
1045 Attributes
|= EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO_16
;
1048 if (Option
== EFI_SET_SUPPORTS
) {
1050 Attributes
|= (UINT64
) (EFI_PCI_IO_ATTRIBUTE_MEMORY_WRITE_COMBINE
|
1051 EFI_PCI_IO_ATTRIBUTE_MEMORY_CACHED
|
1052 EFI_PCI_IO_ATTRIBUTE_MEMORY_DISABLE
|
1053 EFI_PCI_IO_ATTRIBUTE_EMBEDDED_DEVICE
|
1054 EFI_PCI_IO_ATTRIBUTE_EMBEDDED_ROM
|
1055 EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE
);
1057 if (IS_PCI_LPC (&PciIoDevice
->Pci
)) {
1058 Attributes
|= EFI_PCI_IO_ATTRIBUTE_ISA_MOTHERBOARD_IO
;
1059 Attributes
|= (mReserveIsaAliases
? (UINT64
) EFI_PCI_IO_ATTRIBUTE_ISA_IO
: \
1060 (UINT64
) EFI_PCI_IO_ATTRIBUTE_ISA_IO_16
);
1063 if (IS_PCI_BRIDGE (&PciIoDevice
->Pci
) || IS_CARDBUS_BRIDGE (&PciIoDevice
->Pci
)) {
1065 // For bridge, it should support IDE attributes
1067 Attributes
|= EFI_PCI_IO_ATTRIBUTE_IDE_SECONDARY_IO
;
1068 Attributes
|= EFI_PCI_IO_ATTRIBUTE_IDE_PRIMARY_IO
;
1070 if (mReserveVgaAliases
) {
1071 Attributes
&= ~(UINT64
)(EFI_PCI_IO_ATTRIBUTE_VGA_IO_16
| \
1072 EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO_16
);
1074 Attributes
&= ~(UINT64
)(EFI_PCI_IO_ATTRIBUTE_VGA_IO
| \
1075 EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO
);
1079 if (IS_PCI_IDE (&PciIoDevice
->Pci
)) {
1080 Attributes
|= EFI_PCI_IO_ATTRIBUTE_IDE_SECONDARY_IO
;
1081 Attributes
|= EFI_PCI_IO_ATTRIBUTE_IDE_PRIMARY_IO
;
1084 if (IS_PCI_VGA (&PciIoDevice
->Pci
)) {
1085 Attributes
|= EFI_PCI_IO_ATTRIBUTE_VGA_MEMORY
;
1086 Attributes
|= (mReserveVgaAliases
? (UINT64
) EFI_PCI_IO_ATTRIBUTE_VGA_IO
: \
1087 (UINT64
) EFI_PCI_IO_ATTRIBUTE_VGA_IO_16
);
1091 PciIoDevice
->Supports
= Attributes
;
1092 PciIoDevice
->Supports
&= ( (PciIoDevice
->Parent
->Supports
) | \
1093 EFI_PCI_IO_ATTRIBUTE_IO
| EFI_PCI_IO_ATTRIBUTE_MEMORY
| \
1094 EFI_PCI_IO_ATTRIBUTE_BUS_MASTER
);
1098 // When this attribute is clear, the RomImage and RomSize fields in the PCI IO were
1099 // initialized based on the PCI option ROM found through the ROM BAR of the PCI controller.
1100 // When this attribute is set, the PCI option ROM described by the RomImage and RomSize
1101 // fields is not from the the ROM BAR of the PCI controller.
1103 if (!PciIoDevice
->EmbeddedRom
) {
1104 Attributes
|= EFI_PCI_IO_ATTRIBUTE_EMBEDDED_ROM
;
1106 PciIoDevice
->Attributes
= Attributes
;
1111 Determine if the device can support Fast Back to Back attribute.
1113 @param PciIoDevice Pci device instance.
1114 @param StatusIndex Status register value.
1116 @retval EFI_SUCCESS This device support Fast Back to Back attribute.
1117 @retval EFI_UNSUPPORTED This device doesn't support Fast Back to Back attribute.
1121 GetFastBackToBackSupport (
1122 IN PCI_IO_DEVICE
*PciIoDevice
,
1123 IN UINT8 StatusIndex
1126 EFI_PCI_IO_PROTOCOL
*PciIo
;
1128 UINT32 StatusRegister
;
1131 // Read the status register
1133 PciIo
= &PciIoDevice
->PciIo
;
1134 Status
= PciIo
->Pci
.Read (PciIo
, EfiPciIoWidthUint16
, StatusIndex
, 1, &StatusRegister
);
1135 if (EFI_ERROR (Status
)) {
1136 return EFI_UNSUPPORTED
;
1140 // Check the Fast B2B bit
1142 if ((StatusRegister
& EFI_PCI_FAST_BACK_TO_BACK_CAPABLE
) != 0) {
1145 return EFI_UNSUPPORTED
;
1150 Process the option ROM for all the children of the specified parent PCI device.
1151 It can only be used after the first full Option ROM process.
1153 @param PciIoDevice Pci device instance.
1157 ProcessOptionRomLight (
1158 IN PCI_IO_DEVICE
*PciIoDevice
1161 PCI_IO_DEVICE
*Temp
;
1162 LIST_ENTRY
*CurrentLink
;
1165 // For RootBridge, PPB , P2C, go recursively to traverse all its children
1167 CurrentLink
= PciIoDevice
->ChildList
.ForwardLink
;
1168 while (CurrentLink
!= NULL
&& CurrentLink
!= &PciIoDevice
->ChildList
) {
1170 Temp
= PCI_IO_DEVICE_FROM_LINK (CurrentLink
);
1172 if (!IsListEmpty (&Temp
->ChildList
)) {
1173 ProcessOptionRomLight (Temp
);
1176 PciRomGetImageMapping (Temp
);
1179 // The OpRom has already been processed in the first round
1181 Temp
->AllOpRomProcessed
= TRUE
;
1183 CurrentLink
= CurrentLink
->ForwardLink
;
1188 Determine the related attributes of all devices under a Root Bridge.
1190 @param PciIoDevice PCI device instance.
1194 DetermineDeviceAttribute (
1195 IN PCI_IO_DEVICE
*PciIoDevice
1199 UINT16 BridgeControl
;
1201 UINT16 OldBridgeControl
;
1202 BOOLEAN FastB2BSupport
;
1203 PCI_IO_DEVICE
*Temp
;
1204 LIST_ENTRY
*CurrentLink
;
1208 // For Root Bridge, just copy it by RootBridgeIo proctocol
1209 // so as to keep consistent with the actual attribute
1211 if (PciIoDevice
->Parent
== NULL
) {
1212 Status
= PciIoDevice
->PciRootBridgeIo
->GetAttributes (
1213 PciIoDevice
->PciRootBridgeIo
,
1214 &PciIoDevice
->Supports
,
1215 &PciIoDevice
->Attributes
1217 if (EFI_ERROR (Status
)) {
1221 // Assume the PCI Root Bridge supports DAC
1223 PciIoDevice
->Supports
|= (UINT64
)(EFI_PCI_IO_ATTRIBUTE_EMBEDDED_DEVICE
|
1224 EFI_PCI_IO_ATTRIBUTE_EMBEDDED_ROM
|
1225 EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE
);
1230 // Set the attributes to be checked for common PCI devices and PPB or P2C
1231 // Since some devices only support part of them, it is better to set the
1232 // attribute according to its command or bridge control register
1234 Command
= EFI_PCI_COMMAND_IO_SPACE
|
1235 EFI_PCI_COMMAND_MEMORY_SPACE
|
1236 EFI_PCI_COMMAND_BUS_MASTER
|
1237 EFI_PCI_COMMAND_VGA_PALETTE_SNOOP
;
1239 BridgeControl
= EFI_PCI_BRIDGE_CONTROL_ISA
| EFI_PCI_BRIDGE_CONTROL_VGA
| EFI_PCI_BRIDGE_CONTROL_VGA_16
;
1242 // Test whether the device can support attributes above
1244 PciTestSupportedAttribute (PciIoDevice
, &Command
, &BridgeControl
, &OldCommand
, &OldBridgeControl
);
1247 // Set the supported attributes for specified PCI device
1249 PciSetDeviceAttribute (PciIoDevice
, Command
, BridgeControl
, EFI_SET_SUPPORTS
);
1252 // Set the current attributes for specified PCI device
1254 PciSetDeviceAttribute (PciIoDevice
, OldCommand
, OldBridgeControl
, EFI_SET_ATTRIBUTES
);
1257 // Enable other PCI supported attributes but not defined in PCI_IO_PROTOCOL
1258 // For PCI Express devices, Memory Write and Invalidate is hardwired to 0b so only enable it for PCI devices.
1259 if (!PciIoDevice
->IsPciExp
) {
1260 PCI_ENABLE_COMMAND_REGISTER (PciIoDevice
, EFI_PCI_COMMAND_MEMORY_WRITE_AND_INVALIDATE
);
1264 FastB2BSupport
= TRUE
;
1267 // P2C can not support FB2B on the secondary side
1269 if (IS_CARDBUS_BRIDGE (&PciIoDevice
->Pci
)) {
1270 FastB2BSupport
= FALSE
;
1274 // For RootBridge, PPB , P2C, go recursively to traverse all its children
1276 CurrentLink
= PciIoDevice
->ChildList
.ForwardLink
;
1277 while (CurrentLink
!= NULL
&& CurrentLink
!= &PciIoDevice
->ChildList
) {
1279 Temp
= PCI_IO_DEVICE_FROM_LINK (CurrentLink
);
1280 Status
= DetermineDeviceAttribute (Temp
);
1281 if (EFI_ERROR (Status
)) {
1285 // Detect Fast Bact to Bact support for the device under the bridge
1287 Status
= GetFastBackToBackSupport (Temp
, PCI_PRIMARY_STATUS_OFFSET
);
1288 if (FastB2BSupport
&& EFI_ERROR (Status
)) {
1289 FastB2BSupport
= FALSE
;
1292 CurrentLink
= CurrentLink
->ForwardLink
;
1295 // Set or clear Fast Back to Back bit for the whole bridge
1297 if (!IsListEmpty (&PciIoDevice
->ChildList
)) {
1299 if (IS_PCI_BRIDGE (&PciIoDevice
->Pci
)) {
1301 Status
= GetFastBackToBackSupport (PciIoDevice
, PCI_BRIDGE_STATUS_REGISTER_OFFSET
);
1303 if (EFI_ERROR (Status
) || (!FastB2BSupport
)) {
1304 FastB2BSupport
= FALSE
;
1305 PCI_DISABLE_BRIDGE_CONTROL_REGISTER (PciIoDevice
, EFI_PCI_BRIDGE_CONTROL_FAST_BACK_TO_BACK
);
1307 PCI_ENABLE_BRIDGE_CONTROL_REGISTER (PciIoDevice
, EFI_PCI_BRIDGE_CONTROL_FAST_BACK_TO_BACK
);
1311 CurrentLink
= PciIoDevice
->ChildList
.ForwardLink
;
1312 while (CurrentLink
!= NULL
&& CurrentLink
!= &PciIoDevice
->ChildList
) {
1313 Temp
= PCI_IO_DEVICE_FROM_LINK (CurrentLink
);
1314 if (FastB2BSupport
) {
1315 PCI_ENABLE_COMMAND_REGISTER (Temp
, EFI_PCI_COMMAND_FAST_BACK_TO_BACK
);
1317 PCI_DISABLE_COMMAND_REGISTER (Temp
, EFI_PCI_COMMAND_FAST_BACK_TO_BACK
);
1320 CurrentLink
= CurrentLink
->ForwardLink
;
1324 // End for IsListEmpty
1330 This routine is used to update the bar information for those incompatible PCI device.
1332 @param PciIoDevice Input Pci device instance. Output Pci device instance with updated
1335 @retval EFI_SUCCESS Successfully updated bar information.
1336 @retval EFI_UNSUPPORTED Given PCI device doesn't belong to incompatible PCI device list.
1341 IN OUT PCI_IO_DEVICE
*PciIoDevice
1347 VOID
*Configuration
;
1348 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Ptr
;
1350 Configuration
= NULL
;
1351 Status
= EFI_SUCCESS
;
1353 if (gIncompatiblePciDeviceSupport
== NULL
) {
1355 // It can only be supported after the Incompatible PCI Device
1356 // Support Protocol has been installed
1358 Status
= gBS
->LocateProtocol (
1359 &gEfiIncompatiblePciDeviceSupportProtocolGuid
,
1361 (VOID
**) &gIncompatiblePciDeviceSupport
1364 if (Status
== EFI_SUCCESS
) {
1366 // Check whether the device belongs to incompatible devices from protocol or not
1367 // If it is , then get its special requirement in the ACPI table
1369 Status
= gIncompatiblePciDeviceSupport
->CheckDevice (
1370 gIncompatiblePciDeviceSupport
,
1371 PciIoDevice
->Pci
.Hdr
.VendorId
,
1372 PciIoDevice
->Pci
.Hdr
.DeviceId
,
1373 PciIoDevice
->Pci
.Hdr
.RevisionID
,
1374 PciIoDevice
->Pci
.Device
.SubsystemVendorID
,
1375 PciIoDevice
->Pci
.Device
.SubsystemID
,
1381 if (EFI_ERROR (Status
) || Configuration
== NULL
) {
1382 return EFI_UNSUPPORTED
;
1386 // Update PCI device information from the ACPI table
1388 Ptr
= (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*) Configuration
;
1390 while (Ptr
->Desc
!= ACPI_END_TAG_DESCRIPTOR
) {
1392 if (Ptr
->Desc
!= ACPI_ADDRESS_SPACE_DESCRIPTOR
) {
1394 // The format is not support
1399 for (BarIndex
= 0; BarIndex
< PCI_MAX_BAR
; BarIndex
++) {
1400 if ((Ptr
->AddrTranslationOffset
!= MAX_UINT64
) &&
1401 (Ptr
->AddrTranslationOffset
!= MAX_UINT8
) &&
1402 (Ptr
->AddrTranslationOffset
!= BarIndex
)
1405 // Skip updating when AddrTranslationOffset is not MAX_UINT64 or MAX_UINT8 (wide match).
1406 // Skip updating when current BarIndex doesn't equal to AddrTranslationOffset.
1407 // Comparing against MAX_UINT8 is to keep backward compatibility.
1413 switch (Ptr
->ResType
) {
1414 case ACPI_ADDRESS_SPACE_TYPE_MEM
:
1417 // Make sure the bar is memory type
1419 if (CheckBarType (PciIoDevice
, (UINT8
) BarIndex
, PciBarTypeMem
)) {
1423 // Ignored if granularity is 0.
1424 // Ignored if PCI BAR is I/O or 32-bit memory.
1425 // If PCI BAR is 64-bit memory and granularity is 32, then
1426 // the PCI BAR resource is allocated below 4GB.
1427 // If PCI BAR is 64-bit memory and granularity is 64, then
1428 // the PCI BAR resource is allocated above 4GB.
1430 if (PciIoDevice
->PciBar
[BarIndex
].BarType
== PciBarTypeMem64
) {
1431 switch (Ptr
->AddrSpaceGranularity
) {
1433 PciIoDevice
->PciBar
[BarIndex
].BarType
= PciBarTypeMem32
;
1435 PciIoDevice
->PciBar
[BarIndex
].BarTypeFixed
= TRUE
;
1442 if (PciIoDevice
->PciBar
[BarIndex
].BarType
== PciBarTypePMem64
) {
1443 switch (Ptr
->AddrSpaceGranularity
) {
1445 PciIoDevice
->PciBar
[BarIndex
].BarType
= PciBarTypePMem32
;
1447 PciIoDevice
->PciBar
[BarIndex
].BarTypeFixed
= TRUE
;
1456 case ACPI_ADDRESS_SPACE_TYPE_IO
:
1459 // Make sure the bar is IO type
1461 if (CheckBarType (PciIoDevice
, (UINT8
) BarIndex
, PciBarTypeIo
)) {
1470 // Update the new alignment for the device
1472 SetNewAlign (&(PciIoDevice
->PciBar
[BarIndex
].Alignment
), Ptr
->AddrRangeMax
);
1475 // Update the new length for the device
1477 if (Ptr
->AddrLen
!= 0) {
1478 PciIoDevice
->PciBar
[BarIndex
].Length
= Ptr
->AddrLen
;
1486 FreePool (Configuration
);
1492 This routine will update the alignment with the new alignment.
1493 Compare with OLD_ALIGN/EVEN_ALIGN/SQUAD_ALIGN/DQUAD_ALIGN is to keep
1494 backward compatibility.
1496 @param Alignment Input Old alignment. Output updated alignment.
1497 @param NewAlignment New alignment.
1502 IN OUT UINT64
*Alignment
,
1503 IN UINT64 NewAlignment
1506 UINT64 OldAlignment
;
1510 // The new alignment is the same as the original,
1513 if ((NewAlignment
== 0) || (NewAlignment
== OLD_ALIGN
)) {
1517 // Check the validity of the parameter
1519 if (NewAlignment
!= EVEN_ALIGN
&&
1520 NewAlignment
!= SQUAD_ALIGN
&&
1521 NewAlignment
!= DQUAD_ALIGN
) {
1522 *Alignment
= NewAlignment
;
1526 OldAlignment
= (*Alignment
) + 1;
1530 // Get the first non-zero hex value of the length
1532 while ((OldAlignment
& 0x0F) == 0x00) {
1533 OldAlignment
= RShiftU64 (OldAlignment
, 4);
1538 // Adjust the alignment to even, quad or double quad boundary
1540 if (NewAlignment
== EVEN_ALIGN
) {
1541 if ((OldAlignment
& 0x01) != 0) {
1542 OldAlignment
= OldAlignment
+ 2 - (OldAlignment
& 0x01);
1544 } else if (NewAlignment
== SQUAD_ALIGN
) {
1545 if ((OldAlignment
& 0x03) != 0) {
1546 OldAlignment
= OldAlignment
+ 4 - (OldAlignment
& 0x03);
1548 } else if (NewAlignment
== DQUAD_ALIGN
) {
1549 if ((OldAlignment
& 0x07) != 0) {
1550 OldAlignment
= OldAlignment
+ 8 - (OldAlignment
& 0x07);
1555 // Update the old value
1557 NewAlignment
= LShiftU64 (OldAlignment
, ShiftBit
) - 1;
1558 *Alignment
= NewAlignment
;
1564 Parse PCI IOV VF bar information and fill them into PCI device instance.
1566 @param PciIoDevice Pci device instance.
1567 @param Offset Bar offset.
1568 @param BarIndex Bar index.
1570 @return Next bar offset.
1575 IN PCI_IO_DEVICE
*PciIoDevice
,
1581 UINT32 OriginalValue
;
1586 // Ensure it is called properly
1588 ASSERT (PciIoDevice
->SrIovCapabilityOffset
!= 0);
1589 if (PciIoDevice
->SrIovCapabilityOffset
== 0) {
1596 Status
= VfBarExisted (
1603 if (EFI_ERROR (Status
)) {
1604 PciIoDevice
->VfPciBar
[BarIndex
].BaseAddress
= 0;
1605 PciIoDevice
->VfPciBar
[BarIndex
].Length
= 0;
1606 PciIoDevice
->VfPciBar
[BarIndex
].Alignment
= 0;
1609 // Scan all the BARs anyway
1611 PciIoDevice
->VfPciBar
[BarIndex
].Offset
= (UINT16
) Offset
;
1615 PciIoDevice
->VfPciBar
[BarIndex
].Offset
= (UINT16
) Offset
;
1616 if ((Value
& 0x01) != 0) {
1618 // Device I/Os. Impossible
1627 PciIoDevice
->VfPciBar
[BarIndex
].BaseAddress
= OriginalValue
& Mask
;
1629 switch (Value
& 0x07) {
1632 //memory space; anywhere in 32 bit address space
1635 if ((Value
& 0x08) != 0) {
1636 PciIoDevice
->VfPciBar
[BarIndex
].BarType
= PciBarTypePMem32
;
1638 PciIoDevice
->VfPciBar
[BarIndex
].BarType
= PciBarTypeMem32
;
1641 PciIoDevice
->VfPciBar
[BarIndex
].Length
= (~(Value
& Mask
)) + 1;
1642 PciIoDevice
->VfPciBar
[BarIndex
].Alignment
= PciIoDevice
->VfPciBar
[BarIndex
].Length
- 1;
1647 PciIoDevice
->VfPciBar
[BarIndex
].Length
= MultU64x32 (PciIoDevice
->VfPciBar
[BarIndex
].Length
, PciIoDevice
->InitialVFs
);
1651 if (PciIoDevice
->VfPciBar
[BarIndex
].Alignment
< PciIoDevice
->SystemPageSize
- 1) {
1652 PciIoDevice
->VfPciBar
[BarIndex
].Alignment
= PciIoDevice
->SystemPageSize
- 1;
1658 // memory space; anywhere in 64 bit address space
1661 if ((Value
& 0x08) != 0) {
1662 PciIoDevice
->VfPciBar
[BarIndex
].BarType
= PciBarTypePMem64
;
1664 PciIoDevice
->VfPciBar
[BarIndex
].BarType
= PciBarTypeMem64
;
1668 // According to PCI 2.2,if the bar indicates a memory 64 decoding, next bar
1669 // is regarded as an extension for the first bar. As a result
1670 // the sizing will be conducted on combined 64 bit value
1671 // Here just store the masked first 32bit value for future size
1674 PciIoDevice
->VfPciBar
[BarIndex
].Length
= Value
& Mask
;
1675 PciIoDevice
->VfPciBar
[BarIndex
].Alignment
= PciIoDevice
->VfPciBar
[BarIndex
].Length
- 1;
1677 if (PciIoDevice
->VfPciBar
[BarIndex
].Alignment
< PciIoDevice
->SystemPageSize
- 1) {
1678 PciIoDevice
->VfPciBar
[BarIndex
].Alignment
= PciIoDevice
->SystemPageSize
- 1;
1682 // Increment the offset to point to next DWORD
1686 Status
= VfBarExisted (
1693 if (EFI_ERROR (Status
)) {
1698 // Fix the length to support some spefic 64 bit BAR
1700 Value
|= ((UINT32
) -1 << HighBitSet32 (Value
));
1703 // Calculate the size of 64bit bar
1705 PciIoDevice
->VfPciBar
[BarIndex
].BaseAddress
|= LShiftU64 ((UINT64
) OriginalValue
, 32);
1707 PciIoDevice
->VfPciBar
[BarIndex
].Length
= PciIoDevice
->VfPciBar
[BarIndex
].Length
| LShiftU64 ((UINT64
) Value
, 32);
1708 PciIoDevice
->VfPciBar
[BarIndex
].Length
= (~(PciIoDevice
->VfPciBar
[BarIndex
].Length
)) + 1;
1709 PciIoDevice
->VfPciBar
[BarIndex
].Alignment
= PciIoDevice
->VfPciBar
[BarIndex
].Length
- 1;
1714 PciIoDevice
->VfPciBar
[BarIndex
].Length
= MultU64x32 (PciIoDevice
->VfPciBar
[BarIndex
].Length
, PciIoDevice
->InitialVFs
);
1718 if (PciIoDevice
->VfPciBar
[BarIndex
].Alignment
< PciIoDevice
->SystemPageSize
- 1) {
1719 PciIoDevice
->VfPciBar
[BarIndex
].Alignment
= PciIoDevice
->SystemPageSize
- 1;
1728 PciIoDevice
->VfPciBar
[BarIndex
].BarType
= PciBarTypeUnknown
;
1729 PciIoDevice
->VfPciBar
[BarIndex
].Length
= (~(Value
& Mask
)) + 1;
1730 PciIoDevice
->VfPciBar
[BarIndex
].Alignment
= PciIoDevice
->VfPciBar
[BarIndex
].Length
- 1;
1732 if (PciIoDevice
->VfPciBar
[BarIndex
].Alignment
< PciIoDevice
->SystemPageSize
- 1) {
1733 PciIoDevice
->VfPciBar
[BarIndex
].Alignment
= PciIoDevice
->SystemPageSize
- 1;
1741 // Check the length again so as to keep compatible with some special bars
1743 if (PciIoDevice
->VfPciBar
[BarIndex
].Length
== 0) {
1744 PciIoDevice
->VfPciBar
[BarIndex
].BarType
= PciBarTypeUnknown
;
1745 PciIoDevice
->VfPciBar
[BarIndex
].BaseAddress
= 0;
1746 PciIoDevice
->VfPciBar
[BarIndex
].Alignment
= 0;
1750 // Increment number of bar
1756 Parse PCI bar information and fill them into PCI device instance.
1758 @param PciIoDevice Pci device instance.
1759 @param Offset Bar offset.
1760 @param BarIndex Bar index.
1762 @return Next bar offset.
1767 IN PCI_IO_DEVICE
*PciIoDevice
,
1773 UINT32 OriginalValue
;
1780 Status
= BarExisted (
1787 if (EFI_ERROR (Status
)) {
1788 PciIoDevice
->PciBar
[BarIndex
].BaseAddress
= 0;
1789 PciIoDevice
->PciBar
[BarIndex
].Length
= 0;
1790 PciIoDevice
->PciBar
[BarIndex
].Alignment
= 0;
1793 // Some devices don't fully comply to PCI spec 2.2. So be to scan all the BARs anyway
1795 PciIoDevice
->PciBar
[BarIndex
].Offset
= (UINT8
) Offset
;
1799 PciIoDevice
->PciBar
[BarIndex
].BarTypeFixed
= FALSE
;
1800 PciIoDevice
->PciBar
[BarIndex
].Offset
= (UINT8
) Offset
;
1801 if ((Value
& 0x01) != 0) {
1807 if ((Value
& 0xFFFF0000) != 0) {
1811 PciIoDevice
->PciBar
[BarIndex
].BarType
= PciBarTypeIo32
;
1812 PciIoDevice
->PciBar
[BarIndex
].Length
= ((~(Value
& Mask
)) + 1);
1813 PciIoDevice
->PciBar
[BarIndex
].Alignment
= PciIoDevice
->PciBar
[BarIndex
].Length
- 1;
1819 PciIoDevice
->PciBar
[BarIndex
].BarType
= PciBarTypeIo16
;
1820 PciIoDevice
->PciBar
[BarIndex
].Length
= 0x0000FFFF & ((~(Value
& Mask
)) + 1);
1821 PciIoDevice
->PciBar
[BarIndex
].Alignment
= PciIoDevice
->PciBar
[BarIndex
].Length
- 1;
1825 // Workaround. Some platforms inplement IO bar with 0 length
1826 // Need to treat it as no-bar
1828 if (PciIoDevice
->PciBar
[BarIndex
].Length
== 0) {
1829 PciIoDevice
->PciBar
[BarIndex
].BarType
= (PCI_BAR_TYPE
) 0;
1832 PciIoDevice
->PciBar
[BarIndex
].BaseAddress
= OriginalValue
& Mask
;
1838 PciIoDevice
->PciBar
[BarIndex
].BaseAddress
= OriginalValue
& Mask
;
1840 switch (Value
& 0x07) {
1843 //memory space; anywhere in 32 bit address space
1846 if ((Value
& 0x08) != 0) {
1847 PciIoDevice
->PciBar
[BarIndex
].BarType
= PciBarTypePMem32
;
1849 PciIoDevice
->PciBar
[BarIndex
].BarType
= PciBarTypeMem32
;
1852 PciIoDevice
->PciBar
[BarIndex
].Length
= (~(Value
& Mask
)) + 1;
1853 if (PciIoDevice
->PciBar
[BarIndex
].Length
< (SIZE_4KB
)) {
1855 // Force minimum 4KByte alignment for Virtualization technology for Directed I/O
1857 PciIoDevice
->PciBar
[BarIndex
].Alignment
= (SIZE_4KB
- 1);
1859 PciIoDevice
->PciBar
[BarIndex
].Alignment
= PciIoDevice
->PciBar
[BarIndex
].Length
- 1;
1864 // memory space; anywhere in 64 bit address space
1867 if ((Value
& 0x08) != 0) {
1868 PciIoDevice
->PciBar
[BarIndex
].BarType
= PciBarTypePMem64
;
1870 PciIoDevice
->PciBar
[BarIndex
].BarType
= PciBarTypeMem64
;
1874 // According to PCI 2.2,if the bar indicates a memory 64 decoding, next bar
1875 // is regarded as an extension for the first bar. As a result
1876 // the sizing will be conducted on combined 64 bit value
1877 // Here just store the masked first 32bit value for future size
1880 PciIoDevice
->PciBar
[BarIndex
].Length
= Value
& Mask
;
1881 PciIoDevice
->PciBar
[BarIndex
].Alignment
= PciIoDevice
->PciBar
[BarIndex
].Length
- 1;
1884 // Increment the offset to point to next DWORD
1888 Status
= BarExisted (
1895 if (EFI_ERROR (Status
)) {
1897 // the high 32 bit does not claim any BAR, we need to re-check the low 32 bit BAR again
1899 if (PciIoDevice
->PciBar
[BarIndex
].Length
== 0) {
1901 // some device implement MMIO bar with 0 length, need to treat it as no-bar
1903 PciIoDevice
->PciBar
[BarIndex
].BarType
= PciBarTypeUnknown
;
1909 // Fix the length to support some spefic 64 bit BAR
1912 DEBUG ((EFI_D_INFO
, "[PciBus]BAR probing for upper 32bit of MEM64 BAR returns 0, change to 0xFFFFFFFF.\n"));
1913 Value
= (UINT32
) -1;
1915 Value
|= ((UINT32
)(-1) << HighBitSet32 (Value
));
1919 // Calculate the size of 64bit bar
1921 PciIoDevice
->PciBar
[BarIndex
].BaseAddress
|= LShiftU64 ((UINT64
) OriginalValue
, 32);
1923 PciIoDevice
->PciBar
[BarIndex
].Length
= PciIoDevice
->PciBar
[BarIndex
].Length
| LShiftU64 ((UINT64
) Value
, 32);
1924 PciIoDevice
->PciBar
[BarIndex
].Length
= (~(PciIoDevice
->PciBar
[BarIndex
].Length
)) + 1;
1925 if (PciIoDevice
->PciBar
[BarIndex
].Length
< (SIZE_4KB
)) {
1927 // Force minimum 4KByte alignment for Virtualization technology for Directed I/O
1929 PciIoDevice
->PciBar
[BarIndex
].Alignment
= (SIZE_4KB
- 1);
1931 PciIoDevice
->PciBar
[BarIndex
].Alignment
= PciIoDevice
->PciBar
[BarIndex
].Length
- 1;
1940 PciIoDevice
->PciBar
[BarIndex
].BarType
= PciBarTypeUnknown
;
1941 PciIoDevice
->PciBar
[BarIndex
].Length
= (~(Value
& Mask
)) + 1;
1942 if (PciIoDevice
->PciBar
[BarIndex
].Length
< (SIZE_4KB
)) {
1944 // Force minimum 4KByte alignment for Virtualization technology for Directed I/O
1946 PciIoDevice
->PciBar
[BarIndex
].Alignment
= (SIZE_4KB
- 1);
1948 PciIoDevice
->PciBar
[BarIndex
].Alignment
= PciIoDevice
->PciBar
[BarIndex
].Length
- 1;
1955 // Check the length again so as to keep compatible with some special bars
1957 if (PciIoDevice
->PciBar
[BarIndex
].Length
== 0) {
1958 PciIoDevice
->PciBar
[BarIndex
].BarType
= PciBarTypeUnknown
;
1959 PciIoDevice
->PciBar
[BarIndex
].BaseAddress
= 0;
1960 PciIoDevice
->PciBar
[BarIndex
].Alignment
= 0;
1964 // Increment number of bar
1970 This routine is used to initialize the bar of a PCI device.
1972 @param PciIoDevice Pci device instance.
1974 @note It can be called typically when a device is going to be rejected.
1978 InitializePciDevice (
1979 IN PCI_IO_DEVICE
*PciIoDevice
1982 EFI_PCI_IO_PROTOCOL
*PciIo
;
1985 PciIo
= &(PciIoDevice
->PciIo
);
1988 // Put all the resource apertures
1989 // Resource base is set to all ones so as to indicate its resource
1990 // has not been alloacted
1992 for (Offset
= 0x10; Offset
<= 0x24; Offset
+= sizeof (UINT32
)) {
1993 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint32
, Offset
, 1, &gAllOne
);
1998 This routine is used to initialize the bar of a PCI-PCI Bridge device.
2000 @param PciIoDevice PCI-PCI bridge device instance.
2005 IN PCI_IO_DEVICE
*PciIoDevice
2008 EFI_PCI_IO_PROTOCOL
*PciIo
;
2010 PciIo
= &(PciIoDevice
->PciIo
);
2013 // Put all the resource apertures including IO16
2014 // Io32, pMem32, pMem64 to quiescent state
2015 // Resource base all ones, Resource limit all zeros
2017 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint8
, 0x1C, 1, &gAllOne
);
2018 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint8
, 0x1D, 1, &gAllZero
);
2020 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint16
, 0x20, 1, &gAllOne
);
2021 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint16
, 0x22, 1, &gAllZero
);
2023 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint16
, 0x24, 1, &gAllOne
);
2024 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint16
, 0x26, 1, &gAllZero
);
2026 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint32
, 0x28, 1, &gAllOne
);
2027 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint32
, 0x2C, 1, &gAllZero
);
2030 // Don't support use io32 as for now
2032 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint16
, 0x30, 1, &gAllOne
);
2033 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint16
, 0x32, 1, &gAllZero
);
2036 // Force Interrupt line to zero for cards that come up randomly
2038 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint8
, 0x3C, 1, &gAllZero
);
2042 This routine is used to initialize the bar of a PCI Card Bridge device.
2044 @param PciIoDevice PCI Card bridge device.
2049 IN PCI_IO_DEVICE
*PciIoDevice
2052 EFI_PCI_IO_PROTOCOL
*PciIo
;
2054 PciIo
= &(PciIoDevice
->PciIo
);
2057 // Put all the resource apertures including IO16
2058 // Io32, pMem32, pMem64 to quiescent state(
2059 // Resource base all ones, Resource limit all zeros
2061 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint32
, 0x1c, 1, &gAllOne
);
2062 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint32
, 0x20, 1, &gAllZero
);
2064 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint32
, 0x24, 1, &gAllOne
);
2065 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint32
, 0x28, 1, &gAllZero
);
2067 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint32
, 0x2c, 1, &gAllOne
);
2068 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint32
, 0x30, 1, &gAllZero
);
2070 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint32
, 0x34, 1, &gAllOne
);
2071 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint32
, 0x38, 1, &gAllZero
);
2074 // Force Interrupt line to zero for cards that come up randomly
2076 PciIo
->Pci
.Write (PciIo
, EfiPciIoWidthUint8
, 0x3C, 1, &gAllZero
);
2080 Create and initiliaze general PCI I/O device instance for
2081 PCI device/bridge device/hotplug bridge device.
2083 @param PciRootBridgeIo Pointer to instance of EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
2084 @param Pci Input Pci information block.
2085 @param Bus Device Bus NO.
2086 @param Device Device device NO.
2087 @param Func Device func NO.
2089 @return Instance of PCI device. NULL means no instance created.
2094 IN PCI_IO_DEVICE
*Bridge
,
2101 PCI_IO_DEVICE
*PciIoDevice
;
2102 EFI_PCI_IO_PROTOCOL
*PciIo
;
2105 PciIoDevice
= AllocateZeroPool (sizeof (PCI_IO_DEVICE
));
2106 if (PciIoDevice
== NULL
) {
2110 PciIoDevice
->Signature
= PCI_IO_DEVICE_SIGNATURE
;
2111 PciIoDevice
->Handle
= NULL
;
2112 PciIoDevice
->PciRootBridgeIo
= Bridge
->PciRootBridgeIo
;
2113 PciIoDevice
->DevicePath
= NULL
;
2114 PciIoDevice
->BusNumber
= Bus
;
2115 PciIoDevice
->DeviceNumber
= Device
;
2116 PciIoDevice
->FunctionNumber
= Func
;
2117 PciIoDevice
->Decodes
= 0;
2119 if (gFullEnumeration
) {
2120 PciIoDevice
->Allocated
= FALSE
;
2122 PciIoDevice
->Allocated
= TRUE
;
2125 PciIoDevice
->Registered
= FALSE
;
2126 PciIoDevice
->Attributes
= 0;
2127 PciIoDevice
->Supports
= 0;
2128 PciIoDevice
->BusOverride
= FALSE
;
2129 PciIoDevice
->AllOpRomProcessed
= FALSE
;
2131 PciIoDevice
->IsPciExp
= FALSE
;
2133 CopyMem (&(PciIoDevice
->Pci
), Pci
, sizeof (PCI_TYPE01
));
2136 // Initialize the PCI I/O instance structure
2138 InitializePciIoInstance (PciIoDevice
);
2139 InitializePciDriverOverrideInstance (PciIoDevice
);
2140 InitializePciLoadFile2 (PciIoDevice
);
2141 PciIo
= &PciIoDevice
->PciIo
;
2144 // Create a device path for this PCI device and store it into its private data
2146 CreatePciDevicePath (
2152 // Detect if PCI Express Device
2154 PciIoDevice
->PciExpressCapabilityOffset
= 0;
2155 Status
= LocateCapabilityRegBlock (
2157 EFI_PCI_CAPABILITY_ID_PCIEXP
,
2158 &PciIoDevice
->PciExpressCapabilityOffset
,
2161 if (!EFI_ERROR (Status
)) {
2162 PciIoDevice
->IsPciExp
= TRUE
;
2165 if (PcdGetBool (PcdAriSupport
)) {
2167 // Check if the device is an ARI device.
2169 Status
= LocatePciExpressCapabilityRegBlock (
2171 EFI_PCIE_CAPABILITY_ID_ARI
,
2172 &PciIoDevice
->AriCapabilityOffset
,
2175 if (!EFI_ERROR (Status
)) {
2177 // We need to enable ARI feature before calculate BusReservation,
2178 // because FirstVFOffset and VFStride may change after that.
2180 EFI_PCI_IO_PROTOCOL
*ParentPciIo
;
2184 // Check if its parent supports ARI forwarding.
2186 ParentPciIo
= &Bridge
->PciIo
;
2187 ParentPciIo
->Pci
.Read (
2189 EfiPciIoWidthUint32
,
2190 Bridge
->PciExpressCapabilityOffset
+ EFI_PCIE_CAPABILITY_DEVICE_CAPABILITIES_2_OFFSET
,
2194 if ((Data32
& EFI_PCIE_CAPABILITY_DEVICE_CAPABILITIES_2_ARI_FORWARDING
) != 0) {
2196 // ARI forward support in bridge, so enable it.
2198 ParentPciIo
->Pci
.Read (
2200 EfiPciIoWidthUint32
,
2201 Bridge
->PciExpressCapabilityOffset
+ EFI_PCIE_CAPABILITY_DEVICE_CONTROL_2_OFFSET
,
2205 if ((Data32
& EFI_PCIE_CAPABILITY_DEVICE_CONTROL_2_ARI_FORWARDING
) == 0) {
2206 Data32
|= EFI_PCIE_CAPABILITY_DEVICE_CONTROL_2_ARI_FORWARDING
;
2207 ParentPciIo
->Pci
.Write (
2209 EfiPciIoWidthUint32
,
2210 Bridge
->PciExpressCapabilityOffset
+ EFI_PCIE_CAPABILITY_DEVICE_CONTROL_2_OFFSET
,
2216 " ARI: forwarding enabled for PPB[%02x:%02x:%02x]\n",
2218 Bridge
->DeviceNumber
,
2219 Bridge
->FunctionNumber
2224 DEBUG ((EFI_D_INFO
, " ARI: CapOffset = 0x%x\n", PciIoDevice
->AriCapabilityOffset
));
2229 // Initialization for SR-IOV
2232 if (PcdGetBool (PcdSrIovSupport
)) {
2233 Status
= LocatePciExpressCapabilityRegBlock (
2235 EFI_PCIE_CAPABILITY_ID_SRIOV
,
2236 &PciIoDevice
->SrIovCapabilityOffset
,
2239 if (!EFI_ERROR (Status
)) {
2240 UINT32 SupportedPageSize
;
2242 UINT16 FirstVFOffset
;
2248 // If the SR-IOV device is an ARI device, then Set ARI Capable Hierarchy for the device.
2250 if (PcdGetBool (PcdAriSupport
) && PciIoDevice
->AriCapabilityOffset
!= 0) {
2253 EfiPciIoWidthUint16
,
2254 PciIoDevice
->SrIovCapabilityOffset
+ EFI_PCIE_CAPABILITY_ID_SRIOV_CONTROL
,
2258 Data16
|= EFI_PCIE_CAPABILITY_ID_SRIOV_CONTROL_ARI_HIERARCHY
;
2261 EfiPciIoWidthUint16
,
2262 PciIoDevice
->SrIovCapabilityOffset
+ EFI_PCIE_CAPABILITY_ID_SRIOV_CONTROL
,
2269 // Calculate SystemPageSize
2274 EfiPciIoWidthUint32
,
2275 PciIoDevice
->SrIovCapabilityOffset
+ EFI_PCIE_CAPABILITY_ID_SRIOV_SUPPORTED_PAGE_SIZE
,
2279 PciIoDevice
->SystemPageSize
= (PcdGet32 (PcdSrIovSystemPageSize
) & SupportedPageSize
);
2280 ASSERT (PciIoDevice
->SystemPageSize
!= 0);
2284 EfiPciIoWidthUint32
,
2285 PciIoDevice
->SrIovCapabilityOffset
+ EFI_PCIE_CAPABILITY_ID_SRIOV_SYSTEM_PAGE_SIZE
,
2287 &PciIoDevice
->SystemPageSize
2290 // Adjust SystemPageSize for Alignment usage later
2292 PciIoDevice
->SystemPageSize
<<= 12;
2295 // Calculate BusReservation for PCI IOV
2299 // Read First FirstVFOffset, InitialVFs, and VFStride
2303 EfiPciIoWidthUint16
,
2304 PciIoDevice
->SrIovCapabilityOffset
+ EFI_PCIE_CAPABILITY_ID_SRIOV_FIRSTVF
,
2310 EfiPciIoWidthUint16
,
2311 PciIoDevice
->SrIovCapabilityOffset
+ EFI_PCIE_CAPABILITY_ID_SRIOV_INITIALVFS
,
2313 &PciIoDevice
->InitialVFs
2317 EfiPciIoWidthUint16
,
2318 PciIoDevice
->SrIovCapabilityOffset
+ EFI_PCIE_CAPABILITY_ID_SRIOV_VFSTRIDE
,
2325 PFRid
= EFI_PCI_RID(Bus
, Device
, Func
);
2326 LastVF
= PFRid
+ FirstVFOffset
+ (PciIoDevice
->InitialVFs
- 1) * VFStride
;
2329 // Calculate ReservedBusNum for this PF
2331 PciIoDevice
->ReservedBusNum
= (UINT16
)(EFI_PCI_BUS_OF_RID (LastVF
) - Bus
+ 1);
2335 " SR-IOV: SupportedPageSize = 0x%x; SystemPageSize = 0x%x; FirstVFOffset = 0x%x;\n",
2336 SupportedPageSize
, PciIoDevice
->SystemPageSize
>> 12, FirstVFOffset
2340 " InitialVFs = 0x%x; ReservedBusNum = 0x%x; CapOffset = 0x%x\n",
2341 PciIoDevice
->InitialVFs
, PciIoDevice
->ReservedBusNum
, PciIoDevice
->SrIovCapabilityOffset
2346 if (PcdGetBool (PcdMrIovSupport
)) {
2347 Status
= LocatePciExpressCapabilityRegBlock (
2349 EFI_PCIE_CAPABILITY_ID_MRIOV
,
2350 &PciIoDevice
->MrIovCapabilityOffset
,
2353 if (!EFI_ERROR (Status
)) {
2354 DEBUG ((EFI_D_INFO
, " MR-IOV: CapOffset = 0x%x\n", PciIoDevice
->MrIovCapabilityOffset
));
2359 // Initialize the reserved resource list
2361 InitializeListHead (&PciIoDevice
->ReservedResourceList
);
2364 // Initialize the driver list
2366 InitializeListHead (&PciIoDevice
->OptionRomDriverList
);
2369 // Initialize the child list
2371 InitializeListHead (&PciIoDevice
->ChildList
);
2377 This routine is used to enumerate entire pci bus system
2378 in a given platform.
2380 It is only called on the second start on the same Root Bridge.
2382 @param Controller Parent bridge handler.
2384 @retval EFI_SUCCESS PCI enumeration finished successfully.
2385 @retval other Some error occurred when enumerating the pci bus system.
2389 PciEnumeratorLight (
2390 IN EFI_HANDLE Controller
2395 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
2396 PCI_IO_DEVICE
*RootBridgeDev
;
2399 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Descriptors
;
2402 MaxBus
= PCI_MAX_BUS
;
2406 // If this root bridge has been already enumerated, then return successfully
2408 if (GetRootBridgeByHandle (Controller
) != NULL
) {
2413 // Open pci root bridge io protocol
2415 Status
= gBS
->OpenProtocol (
2417 &gEfiPciRootBridgeIoProtocolGuid
,
2418 (VOID
**) &PciRootBridgeIo
,
2419 gPciBusDriverBinding
.DriverBindingHandle
,
2421 EFI_OPEN_PROTOCOL_BY_DRIVER
2423 if (EFI_ERROR (Status
) && Status
!= EFI_ALREADY_STARTED
) {
2427 Status
= PciRootBridgeIo
->Configuration (PciRootBridgeIo
, (VOID
**) &Descriptors
);
2429 if (EFI_ERROR (Status
)) {
2433 while (PciGetBusRange (&Descriptors
, &MinBus
, &MaxBus
, NULL
) == EFI_SUCCESS
) {
2436 // Create a device node for root bridge device with a NULL host bridge controller handle
2438 RootBridgeDev
= CreateRootBridge (Controller
);
2440 if (RootBridgeDev
== NULL
) {
2446 // Record the root bridgeio protocol
2448 RootBridgeDev
->PciRootBridgeIo
= PciRootBridgeIo
;
2450 Status
= PciPciDeviceInfoCollector (
2455 if (!EFI_ERROR (Status
)) {
2458 // Remove those PCI devices which are rejected when full enumeration
2460 RemoveRejectedPciDevices (RootBridgeDev
->Handle
, RootBridgeDev
);
2463 // Process option rom light
2465 ProcessOptionRomLight (RootBridgeDev
);
2468 // Determine attributes for all devices under this root bridge
2470 DetermineDeviceAttribute (RootBridgeDev
);
2473 // If successfully, insert the node into device pool
2475 InsertRootBridge (RootBridgeDev
);
2479 // If unsuccessly, destroy the entire node
2481 DestroyRootBridge (RootBridgeDev
);
2491 Get bus range from PCI resource descriptor list.
2493 @param Descriptors A pointer to the address space descriptor.
2494 @param MinBus The min bus returned.
2495 @param MaxBus The max bus returned.
2496 @param BusRange The bus range returned.
2498 @retval EFI_SUCCESS Successfully got bus range.
2499 @retval EFI_NOT_FOUND Can not find the specific bus.
2504 IN EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
**Descriptors
,
2507 OUT UINT16
*BusRange
2510 while ((*Descriptors
)->Desc
!= ACPI_END_TAG_DESCRIPTOR
) {
2511 if ((*Descriptors
)->ResType
== ACPI_ADDRESS_SPACE_TYPE_BUS
) {
2512 if (MinBus
!= NULL
) {
2513 *MinBus
= (UINT16
) (*Descriptors
)->AddrRangeMin
;
2516 if (MaxBus
!= NULL
) {
2517 *MaxBus
= (UINT16
) (*Descriptors
)->AddrRangeMax
;
2520 if (BusRange
!= NULL
) {
2521 *BusRange
= (UINT16
) (*Descriptors
)->AddrLen
;
2530 return EFI_NOT_FOUND
;
2534 This routine can be used to start the root bridge.
2536 @param RootBridgeDev Pci device instance.
2538 @retval EFI_SUCCESS This device started.
2539 @retval other Failed to get PCI Root Bridge I/O protocol.
2543 StartManagingRootBridge (
2544 IN PCI_IO_DEVICE
*RootBridgeDev
2547 EFI_HANDLE RootBridgeHandle
;
2549 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
2552 // Get the root bridge handle
2554 RootBridgeHandle
= RootBridgeDev
->Handle
;
2555 PciRootBridgeIo
= NULL
;
2558 // Get the pci root bridge io protocol
2560 Status
= gBS
->OpenProtocol (
2562 &gEfiPciRootBridgeIoProtocolGuid
,
2563 (VOID
**) &PciRootBridgeIo
,
2564 gPciBusDriverBinding
.DriverBindingHandle
,
2566 EFI_OPEN_PROTOCOL_BY_DRIVER
2569 if (EFI_ERROR (Status
) && Status
!= EFI_ALREADY_STARTED
) {
2574 // Store the PciRootBridgeIo protocol into root bridge private data
2576 RootBridgeDev
->PciRootBridgeIo
= PciRootBridgeIo
;
2583 This routine can be used to check whether a PCI device should be rejected when light enumeration.
2585 @param PciIoDevice Pci device instance.
2587 @retval TRUE This device should be rejected.
2588 @retval FALSE This device shouldn't be rejected.
2592 IsPciDeviceRejected (
2593 IN PCI_IO_DEVICE
*PciIoDevice
2603 // PPB should be skip!
2605 if (IS_PCI_BRIDGE (&PciIoDevice
->Pci
)) {
2609 if (IS_CARDBUS_BRIDGE (&PciIoDevice
->Pci
)) {
2611 // Only test base registers for P2C
2613 for (BarOffset
= 0x1C; BarOffset
<= 0x38; BarOffset
+= 2 * sizeof (UINT32
)) {
2615 Mask
= (BarOffset
< 0x2C) ? 0xFFFFF000 : 0xFFFFFFFC;
2616 Status
= BarExisted (PciIoDevice
, BarOffset
, &TestValue
, &OldValue
);
2617 if (EFI_ERROR (Status
)) {
2621 TestValue
= TestValue
& Mask
;
2622 if ((TestValue
!= 0) && (TestValue
== (OldValue
& Mask
))) {
2624 // The bar isn't programed, so it should be rejected
2633 for (BarOffset
= 0x14; BarOffset
<= 0x24; BarOffset
+= sizeof (UINT32
)) {
2637 Status
= BarExisted (PciIoDevice
, BarOffset
, &TestValue
, &OldValue
);
2638 if (EFI_ERROR (Status
)) {
2642 if ((TestValue
& 0x01) != 0) {
2648 TestValue
= TestValue
& Mask
;
2649 if ((TestValue
!= 0) && (TestValue
== (OldValue
& Mask
))) {
2659 TestValue
= TestValue
& Mask
;
2661 if ((TestValue
& 0x07) == 0x04) {
2666 BarOffset
+= sizeof (UINT32
);
2667 if ((TestValue
!= 0) && (TestValue
== (OldValue
& Mask
))) {
2670 // Test its high 32-Bit BAR
2672 Status
= BarExisted (PciIoDevice
, BarOffset
, &TestValue
, &OldValue
);
2673 if (TestValue
== OldValue
) {
2683 if ((TestValue
!= 0) && (TestValue
== (OldValue
& Mask
))) {
2694 Reset all bus number from specific bridge.
2696 @param Bridge Parent specific bridge.
2697 @param StartBusNumber Start bus number.
2701 ResetAllPpbBusNumber (
2702 IN PCI_IO_DEVICE
*Bridge
,
2703 IN UINT8 StartBusNumber
2713 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
2715 PciRootBridgeIo
= Bridge
->PciRootBridgeIo
;
2717 for (Device
= 0; Device
<= PCI_MAX_DEVICE
; Device
++) {
2718 for (Func
= 0; Func
<= PCI_MAX_FUNC
; Func
++) {
2721 // Check to see whether a pci device is present
2723 Status
= PciDevicePresent (
2731 if (EFI_ERROR (Status
) && Func
== 0) {
2733 // go to next device if there is no Function 0
2738 if (!EFI_ERROR (Status
) && (IS_PCI_BRIDGE (&Pci
))) {
2741 Address
= EFI_PCI_ADDRESS (StartBusNumber
, Device
, Func
, 0x18);
2742 Status
= PciRootBridgeIo
->Pci
.Read (
2749 SecondaryBus
= (UINT8
)(Register
>> 8);
2751 if (SecondaryBus
!= 0) {
2752 ResetAllPpbBusNumber (Bridge
, SecondaryBus
);
2756 // Reset register 18h, 19h, 1Ah on PCI Bridge
2758 Register
&= 0xFF000000;
2759 Status
= PciRootBridgeIo
->Pci
.Write (
2768 if (Func
== 0 && !IS_PCI_MULTI_FUNC (&Pci
)) {
2770 // Skip sub functions, this is not a multi function device
2772 Func
= PCI_MAX_FUNC
;