Skip to contentSkip to conversation

Xen PCI passthrough fixes

PATCH RESEND Superseded Igor Druzhinin 4 participants 18 messages Mar 6, 2019 – Mar 25, 2019 2 messages not in the archive

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Xen PCI passthrough fixes

Resend to include [email protected] to CC

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 | 44 ++++++++++++++++++++++-----
 1 file changed, 37 insertions(+), 7 deletions(-)
Signature · 7 lines
2.7.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw

PATCH RESEND 1/3 OvmfPkg/XenSupport: remove usage of prefetchable PCI host bridge aperture

This aperture doesn't exist in OVMF and trying to use it causes failing assertions later in cases there are prefetchable and non-prefetchable BARs following each other. This configuration is quite likely with some PCI passthrough devices.

Contributed-under: TianoCore Contribution Agreement 1.1

---
 OvmfPkg/Library/PciHostBridgeLib/XenSupport.c | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)
OvmfPkg/Library/PciHostBridgeLib/XenSupport.c +2 -10
index 9204179..c23c46d 100644
@@ -129,11 +129,7 @@ PcatPciRootBridgeParseBars (
           //
           Length = ((~Length) + 1) & 0xffffffff;
 
-          if ((Value & BIT3) == BIT3) {
-            MemAperture = PMem;
-          } else {
-            MemAperture = Mem;
-          }
+          MemAperture = Mem;
         } else {
           //
           // 64bit
@@ -149,11 +145,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;
Signature · 7 lines
2.7.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#2 lore raw

Re: [PATCH RESEND 1/3] OvmfPkg/XenSupport: remove usage of prefetchable PCI host bridge aperture

Hi,

Quoted 1 line · Igor Druzhinin

This aperture doesn't exist in OVMF and trying to use it causes

I'm trying to understand what you mean by writing "doesn't exist in OVMF". Are prefetchable BAR not handled by ScanForRootBridges() ? Or is it the emulation of the config space that isn't correct? Maybe QEMU should lies about a BAR been prefetchable?

This patch feels like a workaround, but maybe I'm wrong.

Thanks.

Quoted 3 lines

failing assertions later in cases there are prefetchable and non-prefetchable BARs following each other. This configuration is quite likely with some PCI passthrough devices.

Signature · 6 lines
Anthony PERARD

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#3 lore raw

Re: [PATCH RESEND 1/3] OvmfPkg/XenSupport: remove usage of prefetchable PCI host bridge aperture

Quoted 9 lines · Anthony PERARD

Hi,

Quoted 1 line · Igor Druzhinin

This aperture doesn't exist in OVMF and trying to use it causes

I'm trying to understand what you mean by writing "doesn't exist in OVMF". Are prefetchable BAR not handled by ScanForRootBridges() ? Or is it the emulation of the config space that isn't correct? Maybe QEMU should lies about a BAR been prefetchable?

The problem here is: hvmloader places BARs initially disregarding prefetchable bit in an arbitrary order because essentially there is only 1 aperture for the host bridge in emulated system under Xen (and KVM as well). In PcatPciRootBridgeParseBars() we construct apertures for high level OVMF code by reading the BAR placement information after hvmloader. It often appears that there are prefetchable and non-prefetchable BARs coexist with each other and make prefetchable and non-prefetchable apertures overlap. This eventually triggers an assertion in high level OVMF code because that shouldn't happen.

OVMF for KVM is not using prefetchable BAR at all - see PciHostBridgeGetRootBridges() in which it passes mNonExistAperture dummy object to high level code. I think it's wrong to construct a prefetchable aperture for Xen and this code should be removed as it's done for QEMU-KVM. Do you think this patch needs to do that?

Igor

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw

Re: [PATCH RESEND 1/3] OvmfPkg/XenSupport: remove usage of prefetchable PCI host bridge aperture

Quoted 11 lines · Igor Druzhinin
Quoted 9 lines · Anthony PERARD

Hi,

Quoted 1 line · Igor Druzhinin

This aperture doesn't exist in OVMF and trying to use it causes

I'm trying to understand what you mean by writing "doesn't exist in OVMF". Are prefetchable BAR not handled by ScanForRootBridges() ? Or is it the emulation of the config space that isn't correct? Maybe QEMU should lies about a BAR been prefetchable?

Quoted 9 lines

The problem here is: hvmloader places BARs initially disregarding prefetchable bit in an arbitrary order because essentially there is only 1 aperture for the host bridge in emulated system under Xen (and KVM as well). In PcatPciRootBridgeParseBars() we construct apertures for high level OVMF code by reading the BAR placement information after hvmloader. It often appears that there are prefetchable and non-prefetchable BARs coexist with each other and make prefetchable and non-prefetchable apertures overlap. This eventually triggers an assertion in high level OVMF code because that shouldn't happen.

Thanks for the explanation. Could you add it to the patch description?

Quoted 5 lines

OVMF for KVM is not using prefetchable BAR at all - see PciHostBridgeGetRootBridges() in which it passes mNonExistAperture dummy object to high level code. I think it's wrong to construct a prefetchable aperture for Xen and this code should be removed as it's done for QEMU-KVM. Do you think this patch needs to do that?

It would be nice to remove the code that isn't useful so feel free to do it and/or keep the current patch with the description updated.

Thanks,

Signature · 6 lines
Anthony PERARD

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#5 lore raw

Re: [PATCH RESEND 1/3] OvmfPkg/XenSupport: remove usage of prefetchable PCI host bridge aperture

Quoted 24 lines · Anthony PERARD
Quoted 11 lines · Igor Druzhinin
Quoted 9 lines · Anthony PERARD

Hi,

Quoted 1 line · Igor Druzhinin

This aperture doesn't exist in OVMF and trying to use it causes

I'm trying to understand what you mean by writing "doesn't exist in OVMF". Are prefetchable BAR not handled by ScanForRootBridges() ? Or is it the emulation of the config space that isn't correct? Maybe QEMU should lies about a BAR been prefetchable?

Quoted 9 lines

The problem here is: hvmloader places BARs initially disregarding prefetchable bit in an arbitrary order because essentially there is only 1 aperture for the host bridge in emulated system under Xen (and KVM as well). In PcatPciRootBridgeParseBars() we construct apertures for high level OVMF code by reading the BAR placement information after hvmloader. It often appears that there are prefetchable and non-prefetchable BARs coexist with each other and make prefetchable and non-prefetchable apertures overlap. This eventually triggers an assertion in high level OVMF code because that shouldn't happen.

Thanks for the explanation. Could you add it to the patch description?

Quoted 8 lines
Quoted 5 lines

OVMF for KVM is not using prefetchable BAR at all - see PciHostBridgeGetRootBridges() in which it passes mNonExistAperture dummy object to high level code. I think it's wrong to construct a prefetchable aperture for Xen and this code should be removed as it's done for QEMU-KVM. Do you think this patch needs to do that?

It would be nice to remove the code that isn't useful so feel free to do it and/or keep the current patch with the description updated.

Right -- I'd like to add one keyword here, for background: EFI_PCI_HOST_BRIDGE_COMBINE_MEM_PMEM. (It's documented in both edk2 [MdePkg/Include/Protocol/PciHostBridgeResourceAllocation.h] and the PI spec [EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL.GetAllocAttributes()].)

Thanks
Laszlo

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw

Re: [PATCH RESEND 1/3] OvmfPkg/XenSupport: remove usage of prefetchable PCI host bridge aperture

In reply to Igor Druzhinin
Quoted 4 lines · Igor Druzhinin

This aperture doesn't exist in OVMF and trying to use it causes failing assertions later in cases there are prefetchable and non-prefetchable BARs following each other. This configuration is quite likely with some PCI passthrough devices.

According to the PCIe spec, it's fine to place prefetchable BARs in non-prefetchable memory space. There's a note that says that most implementations will only have 1G of non-prefetchable memory, and that most scalable platforms will map 32bit BARs into non-prefetchable memory regardless of the prefetchable bit value.

Shouldn't OVMF be fine with finding both prefetchable and non-prefetchable BARs, as long as the memory region is set to non-prefetchable?

Does OVMF have the capability to position BARs by itself? If so we could skip of the placement done by hvmloader and just let OVMF position things where it see fit.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#7 lore raw

Re: [PATCH RESEND 1/3] OvmfPkg/XenSupport: remove usage of prefetchable PCI host bridge aperture

Quoted 12 lines · Roger Pau Monné
Quoted 4 lines · Igor Druzhinin

This aperture doesn't exist in OVMF and trying to use it causes failing assertions later in cases there are prefetchable and non-prefetchable BARs following each other. This configuration is quite likely with some PCI passthrough devices.

According to the PCIe spec, it's fine to place prefetchable BARs in non-prefetchable memory space. There's a note that says that most implementations will only have 1G of non-prefetchable memory, and that most scalable platforms will map 32bit BARs into non-prefetchable memory regardless of the prefetchable bit value.

Quoted 7 lines

Shouldn't OVMF be fine with finding both prefetchable and non-prefetchable BARs, as long as the memory region is set to non-prefetchable?

Does OVMF have the capability to position BARs by itself? If so we could skip of the placement done by hvmloader and just let OVMF position things where it see fit.

The core PciBusDxe driver that is built into OVMF certainly does the resource allocation/placement, but when OVMF is executed on Xen, this functionality of PciBusDxe is dynamically disabled by setting PcdPciDisableBusEnumeration to TRUE. (I'm not saying this is right vs. wrong, just that it happens.)

Note that OVMF itself checks PcdPciDisableBusEnumeration for many things (just grep OvmfPkg to see), so if we were to flip the PCD while running on Xen, it would change the behavior of OVMF on Xen in a number of areas. Can't offer a deeper treatise for now; all the related source code locations would have to be audited (likely with "git blame" too).

Now, if PciBusDxe *is* allowed/requested to lay out the BARs, through the PCD, then it (indirectly) depends on platform code to provide the resource apertures -- of the root bridges -- out of which it can allocate the BARs. My understanding is that XenSupport.c tries to detect these apertures "retroactively", from the pre-existing BAR placements. This was contributed by Ray in commit 49effaf26ec9 ("OvmfPkg/PciHostBridgeLib: Scan for root bridges when running over Xen", 2016-05-11), so I'll have to defer to him on the code.

I believe that, if we flipped the PCD to FALSE on Xen, and hvmloader would stop pre-configuring the BARs (or OVMF would simply ignore that pre-config), then this code (XenSupport.c) should be possible to eliminate -- *however*, in that case, some other Xen-specific code would become necessary, to expose the root bridge resource apertures (out of which BARs should be allocated by PciBusDxe, see above).

In QEMU's case: all root bridges share the same apertures between each other (given any specific resource type). They are communicated via dynamic PCDs. The 32-bit MMIO aperture PCDs are set in PlatformPei somewhat simply (based on QEMU machine type, IIRC). The 64-bit MMIO aperture PCDs are also calculated in PlatformPei, but that calculation is a *lot* more complex.

All in all, the "root" information is the set of apertures, i.e. what parts of the GPA space can be used for what resource allocation.

Thanks,
Laszlo

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

A message that is not in the archive (sent off-list or not yet received): [email protected]

#9 lore raw

Re: [PATCH RESEND 1/3] OvmfPkg/XenSupport: remove usage of prefetchable PCI host bridge aperture

Quoted 56 lines · Laszlo Ersek
Quoted 12 lines · Roger Pau Monné
Quoted 4 lines · Igor Druzhinin

This aperture doesn't exist in OVMF and trying to use it causes failing assertions later in cases there are prefetchable and non-prefetchable BARs following each other. This configuration is quite likely with some PCI passthrough devices.

According to the PCIe spec, it's fine to place prefetchable BARs in non-prefetchable memory space. There's a note that says that most implementations will only have 1G of non-prefetchable memory, and that most scalable platforms will map 32bit BARs into non-prefetchable memory regardless of the prefetchable bit value.

Quoted 7 lines

Shouldn't OVMF be fine with finding both prefetchable and non-prefetchable BARs, as long as the memory region is set to non-prefetchable?

Does OVMF have the capability to position BARs by itself? If so we could skip of the placement done by hvmloader and just let OVMF position things where it see fit.

The core PciBusDxe driver that is built into OVMF certainly does the resource allocation/placement, but when OVMF is executed on Xen, this functionality of PciBusDxe is dynamically disabled by setting PcdPciDisableBusEnumeration to TRUE. (I'm not saying this is right vs. wrong, just that it happens.)

Note that OVMF itself checks PcdPciDisableBusEnumeration for many things (just grep OvmfPkg to see), so if we were to flip the PCD while running on Xen, it would change the behavior of OVMF on Xen in a number of areas. Can't offer a deeper treatise for now; all the related source code locations would have to be audited (likely with "git blame" too).

Now, if PciBusDxe *is* allowed/requested to lay out the BARs, through the PCD, then it (indirectly) depends on platform code to provide the resource apertures -- of the root bridges -- out of which it can allocate the BARs. My understanding is that XenSupport.c tries to detect these apertures "retroactively", from the pre-existing BAR placements. This was contributed by Ray in commit 49effaf26ec9 ("OvmfPkg/PciHostBridgeLib: Scan for root bridges when running over Xen", 2016-05-11), so I'll have to defer to him on the code.

I believe that, if we flipped the PCD to FALSE on Xen, and hvmloader would stop pre-configuring the BARs (or OVMF would simply ignore that pre-config), then this code (XenSupport.c) should be possible to eliminate -- *however*, in that case, some other Xen-specific code would become necessary, to expose the root bridge resource apertures (out of which BARs should be allocated by PciBusDxe, see above).

In QEMU's case: all root bridges share the same apertures between each other (given any specific resource type). They are communicated via dynamic PCDs. The 32-bit MMIO aperture PCDs are set in PlatformPei somewhat simply (based on QEMU machine type, IIRC). The 64-bit MMIO aperture PCDs are also calculated in PlatformPei, but that calculation is a *lot* more complex.

Quoted 2 lines

All in all, the "root" information is the set of apertures, i.e. what parts of the GPA space can be used for what resource allocation.

Thanks for the detailed explanation. IMO it would be better to let OVMF do the BAR placement instead of having to do it in hvmloader, this just causes code duplication between projects and there's nothing Xen-specific about the PCI resource allocation.

I will try to find some time to look into this, albeit I'm not going to be able to work in this immediately. I'm more than happy if someone else has spare time and wants to pick this up.

Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#10 lore raw

Re: [PATCH RESEND 1/3] OvmfPkg/XenSupport: remove usage of prefetchable PCI host bridge aperture

Quoted 39 lines · Roger Pau Monné
Quoted 35 lines · Laszlo Ersek

The core PciBusDxe driver that is built into OVMF certainly does the resource allocation/placement, but when OVMF is executed on Xen, this functionality of PciBusDxe is dynamically disabled by setting PcdPciDisableBusEnumeration to TRUE. (I'm not saying this is right vs. wrong, just that it happens.)

Note that OVMF itself checks PcdPciDisableBusEnumeration for many things (just grep OvmfPkg to see), so if we were to flip the PCD while running on Xen, it would change the behavior of OVMF on Xen in a number of areas. Can't offer a deeper treatise for now; all the related source code locations would have to be audited (likely with "git blame" too).

Now, if PciBusDxe *is* allowed/requested to lay out the BARs, through the PCD, then it (indirectly) depends on platform code to provide the resource apertures -- of the root bridges -- out of which it can allocate the BARs. My understanding is that XenSupport.c tries to detect these apertures "retroactively", from the pre-existing BAR placements. This was contributed by Ray in commit 49effaf26ec9 ("OvmfPkg/PciHostBridgeLib: Scan for root bridges when running over Xen", 2016-05-11), so I'll have to defer to him on the code.

I believe that, if we flipped the PCD to FALSE on Xen, and hvmloader would stop pre-configuring the BARs (or OVMF would simply ignore that pre-config), then this code (XenSupport.c) should be possible to eliminate -- *however*, in that case, some other Xen-specific code would become necessary, to expose the root bridge resource apertures (out of which BARs should be allocated by PciBusDxe, see above).

In QEMU's case: all root bridges share the same apertures between each other (given any specific resource type). They are communicated via dynamic PCDs. The 32-bit MMIO aperture PCDs are set in PlatformPei somewhat simply (based on QEMU machine type, IIRC). The 64-bit MMIO aperture PCDs are also calculated in PlatformPei, but that calculation is a *lot* more complex.

Quoted 2 lines

All in all, the "root" information is the set of apertures, i.e. what parts of the GPA space can be used for what resource allocation.

Quoted 8 lines

Thanks for the detailed explanation. IMO it would be better to let OVMF do the BAR placement instead of having to do it in hvmloader, this just causes code duplication between projects and there's nothing Xen-specific about the PCI resource allocation.

I will try to find some time to look into this, albeit I'm not going to be able to work in this immediately. I'm more than happy if someone else has spare time and wants to pick this up.

I was thinking about that as well since most of the issues I found stem from the fact hvmloader does its own things behind OVMF's back. But decided that for now it'd be better to have a quick relief than try to change the approach drastically as Laszlo pointed out there are many implications of that change.

So for this patch I prefer to stick to removing only prefetchable aperture all together (as dead code) and marking the host bridge as not supporting it (as Laszlo suggested) since it reflects the reality and done similarly for QEMU-KVM. More code removals and changes could be done later.

Igor

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#11 lore raw

PATCH RESEND 2/3 OvmfPkg/XenSupport: use a correct PCI host bridge aperture for BAR64

In reply to Igor Druzhinin

In case BAR64 is placed below 4G choose the correct aperture. This fixes a failed assertion down the code path.

Contributed-under: TianoCore Contribution Agreement 1.1

---
 OvmfPkg/Library/PciHostBridgeLib/XenSupport.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
OvmfPkg/Library/PciHostBridgeLib/XenSupport.c +5 -1
index c23c46d..408fb24 100644
@@ -145,7 +145,11 @@ PcatPciRootBridgeParseBars (
           Length = Length | LShiftU64 ((UINT64) UpperValue, 32);
           Length = (~Length) + 1;
 
-          MemAperture = MemAbove4G;
+          if (Base < 0x100000000) {
+            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
Acked-by on v1 2/3
#12 lore raw

Re: [PATCH RESEND 2/3] OvmfPkg/XenSupport: use a correct PCI host bridge aperture for BAR64

Quoted 14 lines · Igor Druzhinin

In case BAR64 is placed below 4G choose the correct aperture. This fixes a failed assertion down the code path.

Contributed-under: TianoCore Contribution Agreement 1.1

---
 OvmfPkg/Library/PciHostBridgeLib/XenSupport.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
OvmfPkg/Library/PciHostBridgeLib/XenSupport.c +0 -0
index c23c46d..408fb24 100644

OvmfPkg/Library/PciHostBridgeLib/XenSupport.c PcatPciRootBridgeParseBars ( · line 148

Quoted 5 lines
OvmfPkg/Library/PciHostBridgeLib/XenSupport.c +1 -1
@@ -145,7 +145,11 @@ PcatPciRootBridgeParseBars (
           Length = Length | LShiftU64 ((UINT64) UpperValue, 32);
           Length = (~Length) + 1;
 
-          MemAperture = MemAbove4G;
+          if (Base < 0x100000000) {

You could use the macro BASE_4GB to replace this 1 followed by a looong list of 0.

And with that changed:

Thanks,

Signature · 6 lines
Anthony PERARD

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#13 lore raw

PATCH RESEND 3/3 OvmfPkg/XenSupport: turn off address decoding before BAR sizing

In reply to 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.

Contributed-under: TianoCore Contribution Agreement 1.1

---
 OvmfPkg/Library/PciHostBridgeLib/XenSupport.c | 34 +++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
OvmfPkg/Library/PciHostBridgeLib/XenSupport.c +34 -0
index 408fb24..9940335 100644
@@ -55,6 +55,33 @@ PcatPciRootBridgeBarExisted (
   EnableInterrupts ();
 }
 
+#define EFI_PCI_COMMAND_DECODE ((UINT16)(EFI_PCI_COMMAND_IO_SPACE | \
+                                         EFI_PCI_COMMAND_MEMORY_SPACE))
+STATIC
+VOID
+PcatPciRootBridgeDecoding (
+  IN  UINTN                          Address,
+  IN  BOOLEAN                        Enabled,
+  OUT UINT16                         *OriginalValue
+  )
+{
+  UINT16                            Value;
+
+  //
+  // Preserve the original value
+  //
+  Value = *OriginalValue;
+  *OriginalValue = PciRead16 (Address);
+
+  if (!Enabled) {
+    if (*OriginalValue & EFI_PCI_COMMAND_DECODE)
+       PciWrite16 (Address, *OriginalValue & ~EFI_PCI_COMMAND_DECODE);
+  } else {
+    if (Value & EFI_PCI_COMMAND_DECODE)
+      PciWrite16 (Address, Value);
+  }
+}
+
 STATIC
 VOID
 PcatPciRootBridgeParseBars (
@@ -76,6 +103,7 @@ PcatPciRootBridgeParseBars (
   UINT32                            Value;
   UINT32                            OriginalUpperValue;
   UINT32                            UpperValue;
+  UINT16                            OriginalCommand;
   UINT64                            Mask;
   UINTN                             Offset;
   UINT64                            Base;
@@ -83,6 +111,12 @@ PcatPciRootBridgeParseBars (
   UINT64                            Limit;
   PCI_ROOT_BRIDGE_APERTURE          *MemAperture;
 
+  // Disable address decoding for every device before OVMF starts sizing it
+  PcatPciRootBridgeDecoding (
+    PCI_LIB_ADDRESS (Bus, Device, Function, PCI_COMMAND_OFFSET),
+    FALSE, &OriginalCommand
+  );
+
   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
#14 lore raw

Re: [PATCH RESEND 3/3] OvmfPkg/XenSupport: turn off address decoding before BAR sizing

Quoted 20 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.

Contributed-under: TianoCore Contribution Agreement 1.1

---
 OvmfPkg/Library/PciHostBridgeLib/XenSupport.c | 34 +++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
OvmfPkg/Library/PciHostBridgeLib/XenSupport.c +0 -0
index 408fb24..9940335 100644

OvmfPkg/Library/PciHostBridgeLib/XenSupport.c PcatPciRootBridgeBarExisted ( · line 59

Quoted 5 lines
OvmfPkg/Library/PciHostBridgeLib/XenSupport.c +2 -0
@@ -55,6 +55,33 @@ PcatPciRootBridgeBarExisted (
   EnableInterrupts ();
 }
 
+#define EFI_PCI_COMMAND_DECODE ((UINT16)(EFI_PCI_COMMAND_IO_SPACE | \
+                                         EFI_PCI_COMMAND_MEMORY_SPACE))

I thought I asked you not to define a macro here that started with the "EFI_" prefix :/

http://mid.mail-archive.com/[email protected]

Laszlo

Quoted 40 lines
OvmfPkg/Library/PciHostBridgeLib/XenSupport.c +26 -0
+STATIC
+VOID
+PcatPciRootBridgeDecoding (
+  IN  UINTN                          Address,
+  IN  BOOLEAN                        Enabled,
+  OUT UINT16                         *OriginalValue
+  )
+{
+  UINT16                            Value;
+
+  //
+  // Preserve the original value
+  //
+  Value = *OriginalValue;
+  *OriginalValue = PciRead16 (Address);
+
+  if (!Enabled) {
+    if (*OriginalValue & EFI_PCI_COMMAND_DECODE)
+       PciWrite16 (Address, *OriginalValue & ~EFI_PCI_COMMAND_DECODE);
+  } else {
+    if (Value & EFI_PCI_COMMAND_DECODE)
+      PciWrite16 (Address, Value);
+  }
+}
+
 STATIC
 VOID
 PcatPciRootBridgeParseBars (
@@ -76,6 +103,7 @@ PcatPciRootBridgeParseBars (
   UINT32                            Value;
   UINT32                            OriginalUpperValue;
   UINT32                            UpperValue;
+  UINT16                            OriginalCommand;
   UINT64                            Mask;
   UINTN                             Offset;
   UINT64                            Base;
@@ -83,6 +111,12 @@ PcatPciRootBridgeParseBars (
   UINT64                            Limit;
   PCI_ROOT_BRIDGE_APERTURE          *MemAperture;

OvmfPkg/Library/PciHostBridgeLib/XenSupport.c PcatPciRootBridgeParseBars ( · line 122

Quoted 10 lines
OvmfPkg/Library/PciHostBridgeLib/XenSupport.c +6 -0
 
+  // Disable address decoding for every device before OVMF starts sizing it
+  PcatPciRootBridgeDecoding (
+    PCI_LIB_ADDRESS (Bus, Device, Function, PCI_COMMAND_OFFSET),
+    FALSE, &OriginalCommand
+  );
+
   for (Offset = BarOffsetBase; Offset < BarOffsetEnd; Offset += sizeof (UINT32)) {
     PcatPciRootBridgeBarExisted (
       PCI_LIB_ADDRESS (Bus, Device, Function, Offset),

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

A message that is not in the archive (sent off-list or not yet received): [email protected]

#16 lore raw

Re: [PATCH RESEND 3/3] OvmfPkg/XenSupport: turn off address decoding before BAR sizing

Quoted 24 lines · Laszlo Ersek
Quoted 23 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.

Contributed-under: TianoCore Contribution Agreement 1.1

---
 OvmfPkg/Library/PciHostBridgeLib/XenSupport.c | 34 +++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
OvmfPkg/Library/PciHostBridgeLib/XenSupport.c +0 -0
index 408fb24..9940335 100644
@@ -55,6 +55,33 @@ PcatPciRootBridgeBarExisted (
   EnableInterrupts ();
 }
 
Quoted 8 lines

OvmfPkg/Library/PciHostBridgeLib/XenSupport.c PcatPciRootBridgeBarExisted ( · line 59

Quoted 2 lines
OvmfPkg/Library/PciHostBridgeLib/XenSupport.c +2 -0
+#define EFI_PCI_COMMAND_DECODE ((UINT16)(EFI_PCI_COMMAND_IO_SPACE | \
+                                         EFI_PCI_COMMAND_MEMORY_SPACE))

I thought I asked you not to define a macro here that started with the "EFI_" prefix :/

http://mid.mail-archive.com/[email protected]

This is a resend of v1 patch series to get Xen folks into CC and gather comments. I expect v2.

Igor

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#17 lore raw

Re: [PATCH RESEND 3/3] OvmfPkg/XenSupport: turn off address decoding before BAR sizing

Quoted 31 lines · Igor Druzhinin
Quoted 30 lines · Laszlo Ersek
Quoted 20 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.

Contributed-under: TianoCore Contribution Agreement 1.1

---
 OvmfPkg/Library/PciHostBridgeLib/XenSupport.c | 34 +++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
OvmfPkg/Library/PciHostBridgeLib/XenSupport.c +0 -0
index 408fb24..9940335 100644

OvmfPkg/Library/PciHostBridgeLib/XenSupport.c PcatPciRootBridgeBarExisted ( · line 59

Quoted 5 lines
OvmfPkg/Library/PciHostBridgeLib/XenSupport.c +2 -0
@@ -55,6 +55,33 @@ PcatPciRootBridgeBarExisted (
   EnableInterrupts ();
 }
 
+#define EFI_PCI_COMMAND_DECODE ((UINT16)(EFI_PCI_COMMAND_IO_SPACE | \
+                                         EFI_PCI_COMMAND_MEMORY_SPACE))

I thought I asked you not to define a macro here that started with the "EFI_" prefix :/

Quoted 5 lines
Quoted 2 lines

http://mid.mail-archive.com/[email protected]

This is a resend of v1 patch series to get Xen folks into CC and gather comments. I expect v2.

My bad, thanks for the clarification. On edk2-devel we very rarely post RESENDs without updates, and so I missed the implications now.

Thanks
Laszlo

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#18 lore raw

Re: [PATCH RESEND 3/3] OvmfPkg/XenSupport: turn off address decoding before BAR sizing

In reply to Igor Druzhinin
Quoted 34 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.

Contributed-under: TianoCore Contribution Agreement 1.1

---
 OvmfPkg/Library/PciHostBridgeLib/XenSupport.c | 34 +++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
OvmfPkg/Library/PciHostBridgeLib/XenSupport.c +11 -0
index 408fb24..9940335 100644
@@ -55,6 +55,33 @@ PcatPciRootBridgeBarExisted (
   EnableInterrupts ();
 }
 
+#define EFI_PCI_COMMAND_DECODE ((UINT16)(EFI_PCI_COMMAND_IO_SPACE | \
+                                         EFI_PCI_COMMAND_MEMORY_SPACE))
+STATIC
+VOID
+PcatPciRootBridgeDecoding (
+  IN  UINTN                          Address,
+  IN  BOOLEAN                        Enabled,
+  OUT UINT16                         *OriginalValue
+  )
+{
+  UINT16                            Value;

OvmfPkg/Library/PciHostBridgeLib/XenSupport.c PcatPciRootBridgeBarExisted ( · line 78

Quoted 10 lines
OvmfPkg/Library/PciHostBridgeLib/XenSupport.c +10 -0
+
+  //
+  // Preserve the original value
+  //
+  Value = *OriginalValue;
+  *OriginalValue = PciRead16 (Address);
+
+  if (!Enabled) {
+    if (*OriginalValue & EFI_PCI_COMMAND_DECODE)
+       PciWrite16 (Address, *OriginalValue & ~EFI_PCI_COMMAND_DECODE);

The edk2 coding style ask to always use { } with if:
https://github.com/tianocore/tianocore.github.io/wiki/Code-Style-C

OvmfPkg/Library/PciHostBridgeLib/XenSupport.c PcatPciRootBridgeBarExisted ( · line 81

Quoted 3 lines
OvmfPkg/Library/PciHostBridgeLib/XenSupport.c +3 -0
+  } else {
+    if (Value & EFI_PCI_COMMAND_DECODE)
+      PciWrite16 (Address, Value);

here too.

Quoted 15 lines
OvmfPkg/Library/PciHostBridgeLib/XenSupport.c +4 -0
+  }
+}
+
 STATIC
 VOID
 PcatPciRootBridgeParseBars (
@@ -76,6 +103,7 @@ PcatPciRootBridgeParseBars (
   UINT32                            Value;
   UINT32                            OriginalUpperValue;
   UINT32                            UpperValue;
+  UINT16                            OriginalCommand;
   UINT64                            Mask;
   UINTN                             Offset;
   UINT64                            Base;

OvmfPkg/Library/PciHostBridgeLib/XenSupport.c PcatPciRootBridgeParseBars ( · line 117

Quoted 7 lines
OvmfPkg/Library/PciHostBridgeLib/XenSupport.c +4 -0
@@ -83,6 +111,12 @@ PcatPciRootBridgeParseBars (
   UINT64                            Limit;
   PCI_ROOT_BRIDGE_APERTURE          *MemAperture;
 
+  // Disable address decoding for every device before OVMF starts sizing it
+  PcatPciRootBridgeDecoding (
+    PCI_LIB_ADDRESS (Bus, Device, Function, PCI_COMMAND_OFFSET),
+    FALSE, &OriginalCommand

Is it on purpose that the original register value is stored in OriginalCommand, but never used again?

OvmfPkg/Library/PciHostBridgeLib/XenSupport.c PcatPciRootBridgeParseBars ( · line 122

Quoted 5 lines
OvmfPkg/Library/PciHostBridgeLib/XenSupport.c +2 -0
+  );
+
   for (Offset = BarOffsetBase; Offset < BarOffsetEnd; Offset += sizeof (UINT32)) {
     PcatPciRootBridgeBarExisted (
       PCI_LIB_ADDRESS (Bus, Device, Function, Offset),

Thanks,

Signature · 6 lines
Anthony PERARD

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#19 lore raw

Re: [PATCH RESEND 3/3] OvmfPkg/XenSupport: turn off address decoding before BAR sizing

Quoted 73 lines · Anthony PERARD
Quoted 34 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.

Contributed-under: TianoCore Contribution Agreement 1.1

---
 OvmfPkg/Library/PciHostBridgeLib/XenSupport.c | 34 +++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
OvmfPkg/Library/PciHostBridgeLib/XenSupport.c +11 -0
index 408fb24..9940335 100644
@@ -55,6 +55,33 @@ PcatPciRootBridgeBarExisted (
   EnableInterrupts ();
 }
 
+#define EFI_PCI_COMMAND_DECODE ((UINT16)(EFI_PCI_COMMAND_IO_SPACE | \
+                                         EFI_PCI_COMMAND_MEMORY_SPACE))
+STATIC
+VOID
+PcatPciRootBridgeDecoding (
+  IN  UINTN                          Address,
+  IN  BOOLEAN                        Enabled,
+  OUT UINT16                         *OriginalValue
+  )
+{
+  UINT16                            Value;

OvmfPkg/Library/PciHostBridgeLib/XenSupport.c PcatPciRootBridgeBarExisted ( · line 78

Quoted 10 lines
OvmfPkg/Library/PciHostBridgeLib/XenSupport.c +10 -0
+
+  //
+  // Preserve the original value
+  //
+  Value = *OriginalValue;
+  *OriginalValue = PciRead16 (Address);
+
+  if (!Enabled) {
+    if (*OriginalValue & EFI_PCI_COMMAND_DECODE)
+       PciWrite16 (Address, *OriginalValue & ~EFI_PCI_COMMAND_DECODE);

The edk2 coding style ask to always use { } with if:
https://github.com/tianocore/tianocore.github.io/wiki/Code-Style-C

OvmfPkg/Library/PciHostBridgeLib/XenSupport.c PcatPciRootBridgeBarExisted ( · line 81

Quoted 3 lines
OvmfPkg/Library/PciHostBridgeLib/XenSupport.c +3 -0
+  } else {
+    if (Value & EFI_PCI_COMMAND_DECODE)
+      PciWrite16 (Address, Value);

here too.

Quoted 18 lines
OvmfPkg/Library/PciHostBridgeLib/XenSupport.c +4 -0
+  }
+}
+
 STATIC
 VOID
 PcatPciRootBridgeParseBars (
@@ -76,6 +103,7 @@ PcatPciRootBridgeParseBars (
   UINT32                            Value;
   UINT32                            OriginalUpperValue;
   UINT32                            UpperValue;
+  UINT16                            OriginalCommand;
   UINT64                            Mask;
   UINTN                             Offset;
   UINT64                            Base;
@@ -83,6 +111,12 @@ PcatPciRootBridgeParseBars (
   UINT64                            Limit;
   PCI_ROOT_BRIDGE_APERTURE          *MemAperture;
 
Quoted 7 lines

OvmfPkg/Library/PciHostBridgeLib/XenSupport.c PcatPciRootBridgeParseBars ( · line 117

Quoted 4 lines
OvmfPkg/Library/PciHostBridgeLib/XenSupport.c +4 -0
+  // Disable address decoding for every device before OVMF starts sizing it
+  PcatPciRootBridgeDecoding (
+    PCI_LIB_ADDRESS (Bus, Device, Function, PCI_COMMAND_OFFSET),
+    FALSE, &OriginalCommand

Is it on purpose that the original register value is stored in OriginalCommand, but never used again?

Those are probably remnants of extended patch version for XenServer - will remove together with other suggestions from you and Laszlo.

Igor

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel