Xen PCI passthrough fixes
cover Xen PCI passthrough fixes
Igor Druzhinin (3):
OvmfPkg/XenSupport: remove usage of prefetchable PCI host bridge
aperture
OvmfPkg/XenSupport: use a correct PCI host bridge aperture for BAR64 OvmfPkg/XenSupport: turn off address decoding before BAR sizing
OvmfPkg/Library/PciHostBridgeLib/XenSupport.c | 53 +++++++++++++++++---------- 1 file changed, 34 insertions(+), 19 deletions(-)
Signature · 7 lines
2.7.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v3 1/3 OvmfPkg/XenSupport: remove usage of prefetchable PCI host bridge aperture
This aperture doesn't exist in QEMU-XEN and hvmloader places BARs in arbitrary order disregarding prefetchable bit. This makes prefetchable and non-prefetchable BARs to follow each other that's quite likely with PCI passthrough devices. In that case, the existing code, that tries to work out aperture boundaries by reading hvmloader BAR placement, will report a bogus prefetchable aperture which overlaps with the regular one. It will eventually trigger an assertion in DXE PCI initialization code.
Do the same thing as OVMF on QEMU-KVM and pass a non-existing aperture there. It's not necessary to pass additional allocation flags as we set ResourceAssigned flag on the root bridge which means they will be ignored.
--- OvmfPkg/Library/PciHostBridgeLib/XenSupport.c | 34 ++++++++++----------------- 1 file changed, 12 insertions(+), 22 deletions(-)
OvmfPkg/Library/PciHostBridgeLib/XenSupport.c +12 -22
@@ -60,9 +60,7 @@ PcatPciRootBridgeParseBars ( IN UINTN BarOffsetEnd, IN PCI_ROOT_BRIDGE_APERTURE *Io, IN PCI_ROOT_BRIDGE_APERTURE *Mem, - IN PCI_ROOT_BRIDGE_APERTURE *MemAbove4G, - IN PCI_ROOT_BRIDGE_APERTURE *PMem, - IN PCI_ROOT_BRIDGE_APERTURE *PMemAbove4G + IN PCI_ROOT_BRIDGE_APERTURE *MemAbove4G ) {
@@ -123,11 +121,7 @@ PcatPciRootBridgeParseBars (// Length = ((~Length) +1 ) &0xffffffff ; -if ((Value &BIT3 ) ==BIT3 ) { - MemAperture = PMem; - }else { - MemAperture = Mem; - } + MemAperture = Mem; }else {// // 64bit
@@ -143,11 +137,7 @@ PcatPciRootBridgeParseBars ( Length = Length |LShiftU64 ((UINT64 ) UpperValue,32 ); Length = (~Length) +1 ; -if ((Value &BIT3 ) ==BIT3 ) { - MemAperture = PMemAbove4G; - }else { - MemAperture = MemAbove4G; - } + MemAperture = MemAbove4G; } Limit = Base + Length -1 ;
@@ -164,6 +154,8 @@ PcatPciRootBridgeParseBars ( } } +STATIC PCI_ROOT_BRIDGE_APERTURE mNonExistAperture = { MAX_UINT64, 0 }; +PCI_ROOT_BRIDGE *ScanForRootBridges ( UINTN *NumberOfRootBridges
@@ -180,7 +172,7 @@ ScanForRootBridges (UINT64 Base;UINT64 Limit;UINT64 Value; -PCI_ROOT_BRIDGE_APERTURE Io, Mem, MemAbove4G, PMem, PMemAbove4G, *MemAperture; +PCI_ROOT_BRIDGE_APERTURE Io, Mem, MemAbove4G, *MemAperture;PCI_ROOT_BRIDGE *RootBridges;UINTN BarOffsetEnd;
@@ -200,9 +192,7 @@ ScanForRootBridges (ZeroMem (&Io,sizeof (Io));ZeroMem (&Mem,sizeof (Mem));ZeroMem (&MemAbove4G,sizeof (MemAbove4G)); -ZeroMem (&PMem,sizeof (PMem)); -ZeroMem (&PMemAbove4G,sizeof (PMemAbove4G)); - Io.Base = Mem.Base = MemAbove4G.Base = PMem.Base = PMemAbove4G.Base =MAX_UINT64 ; + Io.Base = Mem.Base = MemAbove4G.Base =MAX_UINT64 ;// // Scan all the PCI devices on the primary bus of the PCI root bridge //
@@ -307,16 +297,17 @@ ScanForRootBridges (// // Get the Prefetchable Memory range that the PPB is decoding +// and merge it into Memory range // Value = Pci.Bridge.PrefetchableMemoryBase &0x0f ; Base = ((UINT32 ) Pci.Bridge.PrefetchableMemoryBase &0xfff0 ) <<16 ; Limit = (((UINT32 ) Pci.Bridge.PrefetchableMemoryLimit &0xfff0 ) <<16 ) |0xfffff ; - MemAperture = &PMem; + MemAperture = &Mem;if (Value ==BIT0 ) { Base |=LShiftU64 (Pci.Bridge.PrefetchableBaseUpper32,32 ); Limit |=LShiftU64 (Pci.Bridge.PrefetchableLimitUpper32,32 ); - MemAperture = &PMemAbove4G; + MemAperture = &MemAbove4G; }if (Base < Limit) {if (MemAperture->Base > Base) {
@@ -367,8 +358,7 @@ ScanForRootBridges (OFFSET_OF (PCI_TYPE00 , Device.Bar), BarOffsetEnd, &Io, - &Mem, &MemAbove4G, - &PMem, &PMemAbove4G + &Mem, &MemAbove4G );//
@@ -440,7 +430,7 @@ ScanForRootBridges (InitRootBridge ( Attributes, Attributes,0 , (UINT8 ) PrimaryBus, (UINT8 ) SubBus, - &Io, &Mem, &MemAbove4G, &PMem, &PMemAbove4G, + &Io, &Mem, &MemAbove4G, &mNonExistAperture, &mNonExistAperture, &RootBridges[*NumberOfRootBridges] ); RootBridges[*NumberOfRootBridges].ResourceAssigned = TRUE;
Signature · 7 lines
2.7.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v3 2/3 OvmfPkg/XenSupport: use a correct PCI host bridge aperture for BAR64
In reply to Igor DruzhininIn case BAR64 is placed below 4G choose the correct aperture. This fixes a failed assertion down the code path.
--- OvmfPkg/Library/PciHostBridgeLib/XenSupport.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
OvmfPkg/Library/PciHostBridgeLib/XenSupport.c +5 -1
@@ -137,7 +137,11 @@ PcatPciRootBridgeParseBars ( Length = Length |LShiftU64 ((UINT64 ) UpperValue,32 ); Length = (~Length) +1 ; - MemAperture = MemAbove4G; +if (Base <BASE_4GB ) { + MemAperture = Mem; + }else { + MemAperture = MemAbove4G; + } } Limit = Base + Length -1 ;
Signature · 7 lines
2.7.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v3 3/3 OvmfPkg/XenSupport: turn off address decoding before BAR sizing
In reply to Igor DruzhininOn Xen, hvmloader firmware leaves address decoding enabled for enumerated PCI device before jumping into OVMF. OVMF seems to expect it to be disabled and tries to size PCI BARs in several places without disabling it which causes BAR64, for example, being incorrectly placed by QEMU.
Fix it by disabling PCI address decoding explicitly before the first attempt to size BARs on Xen.
---
Changes in v3:
- dropped unused arguments and rename PcatPciRootBridgeDecoding function
- make mask application more clear as suggested
--- OvmfPkg/Library/PciHostBridgeLib/XenSupport.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
OvmfPkg/Library/PciHostBridgeLib/XenSupport.c +21 -0
@@ -49,6 +49,22 @@ PcatPciRootBridgeBarExisted (EnableInterrupts (); } +#define PCI_COMMAND_DECODE ((UINT16)(EFI_PCI_COMMAND_IO_SPACE | \ + EFI_PCI_COMMAND_MEMORY_SPACE)) +STATIC +VOID +PcatPciRootBridgeDecodingDisable ( + IN UINTN Address + ) +{ +UINT16 Value; + + Value =PciRead16 (Address); +if (Value &PCI_COMMAND_DECODE ) { +PciWrite16 (Address, Value & ~(UINT32 )PCI_COMMAND_DECODE ); + } +} +STATIC VOID PcatPciRootBridgeParseBars (
@@ -75,6 +91,11 @@ PcatPciRootBridgeParseBars (UINT64 Limit;PCI_ROOT_BRIDGE_APERTURE *MemAperture; +// Disable address decoding for every device before OVMF starts sizing it +PcatPciRootBridgeDecodingDisable ( +PCI_LIB_ADDRESS (Bus, Device, Function,PCI_COMMAND_OFFSET ) + ); +for (Offset = BarOffsetBase; Offset < BarOffsetEnd; Offset +=sizeof (UINT32 )) { PcatPciRootBridgeBarExisted ( PCI_LIB_ADDRESS (Bus, Device, Function, Offset),
Signature · 7 lines
2.7.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH v3 3/3] OvmfPkg/XenSupport: turn off address decoding before BAR sizing
Quoted 48 lines · Igor Druzhinin
On Xen, hvmloader firmware leaves address decoding enabled for enumerated PCI device before jumping into OVMF. OVMF seems to expect it to be disabled and tries to size PCI BARs in several places without disabling it which causes BAR64, for example, being incorrectly placed by QEMU.
Fix it by disabling PCI address decoding explicitly before the first attempt to size BARs on Xen.
Signed-off-by Igor Druzhinin <[email protected]>---
Changes in v3:
- dropped unused arguments and rename PcatPciRootBridgeDecoding function
- make mask application more clear as suggested--- OvmfPkg/Library/PciHostBridgeLib/XenSupport.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)OvmfPkg/Library/PciHostBridgeLib/XenSupport.c +16
-0@@ -49,6 +49,22 @@ PcatPciRootBridgeBarExisted (EnableInterrupts (); } +#define PCI_COMMAND_DECODE ((UINT16)(EFI_PCI_COMMAND_IO_SPACE | \ + EFI_PCI_COMMAND_MEMORY_SPACE)) +STATIC +VOID +PcatPciRootBridgeDecodingDisable ( + IN UINTN Address + ) +{ +UINT16 Value; + + Value =PciRead16 (Address); +if (Value &PCI_COMMAND_DECODE ) { +PciWrite16 (Address, Value & ~(UINT32 )PCI_COMMAND_DECODE ); + } +} +STATIC VOID PcatPciRootBridgeParseBars (@@ -75,6 +91,11 @@ PcatPciRootBridgeParseBars (UINT64 Limit;
OvmfPkg/Library/PciHostBridgeLib/XenSupport.c PcatPciRootBridgeParseBars ( · line 101
Quoted 10 lines
OvmfPkg/Library/PciHostBridgeLib/XenSupport.c +5
-0PCI_ROOT_BRIDGE_APERTURE *MemAperture; +// Disable address decoding for every device before OVMF starts sizing it +PcatPciRootBridgeDecodingDisable ( +PCI_LIB_ADDRESS (Bus, Device, Function,PCI_COMMAND_OFFSET ) + ); +for (Offset = BarOffsetBase; Offset < BarOffsetEnd; Offset +=sizeof (UINT32 )) { PcatPciRootBridgeBarExisted ( PCI_LIB_ADDRESS (Bus, Device, Function, Offset),
Someone from the Xen community please ACK this too, and then we can push the series.
Thanks,
Laszlo
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Anthony PERARD
Acked-byRe: [PATCH v3 3/3] OvmfPkg/XenSupport: turn off address decoding before BAR sizing
Quoted 62 lines · Laszlo Ersek
Quoted 48 lines · Igor Druzhinin
On Xen, hvmloader firmware leaves address decoding enabled for enumerated PCI device before jumping into OVMF. OVMF seems to expect it to be disabled and tries to size PCI BARs in several places without disabling it which causes BAR64, for example, being incorrectly placed by QEMU.
Fix it by disabling PCI address decoding explicitly before the first attempt to size BARs on Xen.
Signed-off-by Igor Druzhinin <[email protected]>---
Changes in v3:
- dropped unused arguments and rename PcatPciRootBridgeDecoding function
- make mask application more clear as suggested--- OvmfPkg/Library/PciHostBridgeLib/XenSupport.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)OvmfPkg/Library/PciHostBridgeLib/XenSupport.c +16
-0@@ -49,6 +49,22 @@ PcatPciRootBridgeBarExisted (EnableInterrupts (); } +#define PCI_COMMAND_DECODE ((UINT16)(EFI_PCI_COMMAND_IO_SPACE | \ + EFI_PCI_COMMAND_MEMORY_SPACE)) +STATIC +VOID +PcatPciRootBridgeDecodingDisable ( + IN UINTN Address + ) +{ +UINT16 Value; + + Value =PciRead16 (Address); +if (Value &PCI_COMMAND_DECODE ) { +PciWrite16 (Address, Value & ~(UINT32 )PCI_COMMAND_DECODE ); + } +} +STATIC VOID PcatPciRootBridgeParseBars (@@ -75,6 +91,11 @@ PcatPciRootBridgeParseBars (UINT64 Limit;OvmfPkg/Library/PciHostBridgeLib/XenSupport.c PcatPciRootBridgeParseBars ( · line 101
Quoted 10 lines
OvmfPkg/Library/PciHostBridgeLib/XenSupport.c +5
-0PCI_ROOT_BRIDGE_APERTURE *MemAperture; +// Disable address decoding for every device before OVMF starts sizing it +PcatPciRootBridgeDecodingDisable ( +PCI_LIB_ADDRESS (Bus, Device, Function,PCI_COMMAND_OFFSET ) + ); +for (Offset = BarOffsetBase; Offset < BarOffsetEnd; Offset +=sizeof (UINT32 )) { PcatPciRootBridgeBarExisted ( PCI_LIB_ADDRESS (Bus, Device, Function, Offset),Acked-by Laszlo Ersek <[email protected]>
Quoted 2 lines
Someone from the Xen community please ACK this too, and then we can push the series.
Thanks,
Signature · 6 lines
Anthony PERARD _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [edk2-devel] [PATCH v3 3/3] OvmfPkg/XenSupport: turn off address decoding before BAR sizing
Quoted 63 lines · Anthony PERARD
Quoted 62 lines · Laszlo Ersek
Quoted 48 lines · Igor Druzhinin
On Xen, hvmloader firmware leaves address decoding enabled for enumerated PCI device before jumping into OVMF. OVMF seems to expect it to be disabled and tries to size PCI BARs in several places without disabling it which causes BAR64, for example, being incorrectly placed by QEMU.
Fix it by disabling PCI address decoding explicitly before the first attempt to size BARs on Xen.
Signed-off-by Igor Druzhinin <[email protected]>---
Changes in v3:
- dropped unused arguments and rename PcatPciRootBridgeDecoding function
- make mask application more clear as suggested--- OvmfPkg/Library/PciHostBridgeLib/XenSupport.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)OvmfPkg/Library/PciHostBridgeLib/XenSupport.c +16
-0@@ -49,6 +49,22 @@ PcatPciRootBridgeBarExisted (EnableInterrupts (); } +#define PCI_COMMAND_DECODE ((UINT16)(EFI_PCI_COMMAND_IO_SPACE | \ + EFI_PCI_COMMAND_MEMORY_SPACE)) +STATIC +VOID +PcatPciRootBridgeDecodingDisable ( + IN UINTN Address + ) +{ +UINT16 Value; + + Value =PciRead16 (Address); +if (Value &PCI_COMMAND_DECODE ) { +PciWrite16 (Address, Value & ~(UINT32 )PCI_COMMAND_DECODE ); + } +} +STATIC VOID PcatPciRootBridgeParseBars (@@ -75,6 +91,11 @@ PcatPciRootBridgeParseBars (UINT64 Limit;OvmfPkg/Library/PciHostBridgeLib/XenSupport.c PcatPciRootBridgeParseBars ( · line 101
Quoted 10 lines
OvmfPkg/Library/PciHostBridgeLib/XenSupport.c +5
-0PCI_ROOT_BRIDGE_APERTURE *MemAperture; +// Disable address decoding for every device before OVMF starts sizing it +PcatPciRootBridgeDecodingDisable ( +PCI_LIB_ADDRESS (Bus, Device, Function,PCI_COMMAND_OFFSET ) + ); +for (Offset = BarOffsetBase; Offset < BarOffsetEnd; Offset +=sizeof (UINT32 )) { PcatPciRootBridgeBarExisted ( PCI_LIB_ADDRESS (Bus, Device, Function, Offset),Acked-by Laszlo Ersek <[email protected]>
Quoted 4 lines
Quoted 2 lines
Someone from the Xen community please ACK this too, and then we can push the series.
Acked-by Anthony PERARD <[email protected]>
Series pushed as commit range 9fb2ce2f465d..0c40c9c925ca.
Thanks
Laszlo
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel