Skip to contentSkip to conversation

PCI devices passthrough on Arm

PATCH In review Rahul Singh 12 participants 190 messages Oct 6, 2021 – Oct 21, 2021 Arm/Zynq architecture Page 2 of 2

lore
Share & tools
lore
#149 lore raw

Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM.

In reply to Bertrand Marquis
Quoted 9 lines · Bertrand Marquis
xen/drivers/passthrough/pci.c +5 -0
@@ -752,6 +752,19 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
 
     check_pdev(pdev);
 
+#ifdef CONFIG_ARM
+    /*
+     * On ARM PCI devices discovery will be done by Dom0. Add vpci handler when
+     * Dom0 inform XEN to add the PCI devices in XEN.
+     */

xen/drivers/passthrough/pci.c int pci_add_device(u16 seg, u8 bus, u8 devfn, · line 769

Quoted 10 lines
xen/drivers/passthrough/pci.c +8 -0
+    ret = vpci_add_handlers(pdev);
+    if ( ret )
+    {
+        printk(XENLOG_ERR "Setup of vPCI failed: %d\n", ret);
+        goto out;
+    }
+#endif
+
     ret = 0;
     if ( !pdev->domain )

Elsewhere I did point out that you need to be careful wrt pdev->domain being NULL. As the code in context clearly documents, you're now adding handlers before that field was set. In comments to a prior version I did already suggest to consider placing the new code inside the if() in question (albeit at the time this was mainly to make clear that error cleanup may not fiddle with things not put in place by the iommu_enable_device() alternative path). This would have the further benefit of making is crystal clear that here only handlers for Dom0 would get put in place (and hence their installing for DomU-s would need to be looked for elsewhere).

xen/drivers/passthrough/pci.c out: · line 801

Quoted 6 lines
xen/drivers/passthrough/pci.c +2 -0
@@ -784,6 +797,9 @@ out:
                    &PCI_SBDF(seg, bus, slot, func));
         }
     }
+    else if ( pdev )
+        pci_cleanup_msi(pdev);

Have you thoroughly checked that this call is benign on x86? There's no wording to that effect in the description afaics, and I can't easily convince myself that it would be correct when the iommu_enable_device() path was taken. (I'm also not going to exclude that it should have been there even prior to your work, albeit then adding this would want to be a separate bugfix patch.)

xen/drivers/vpci/header.c static void bar_write(const struct pci_dev *pdev, unsigned int reg, · line 378

Quoted 9 lines
xen/drivers/vpci/header.c +2 -1
@@ -374,7 +374,8 @@ static void bar_write(const struct pci_dev *pdev, unsigned int reg,
         if ( val != (uint32_t)(bar->addr >> (hi ? 32 : 0)) )
             gprintk(XENLOG_WARNING,
                     "%pp: ignored BAR %lu write with memory decoding enabled\n",
-                    &pdev->sbdf, bar - pdev->vpci->header.bars + hi);
+                    &pdev->sbdf,
+                    (unsigned long)(bar - pdev->vpci->header.bars + hi));

This looks like an entirely unrelated change which I'm afraid I don't even understand why it needs making. The description says this is for Arm32, but it remains unclear what the compilation error would have been. My best guess is that perhaps you really mean to change the format specifier to %zu (really this should be %td, but our vsprintf() doesn't support 't' for whatever reason).

Please recall that we try to avoid casts where possible.

xen/drivers/vpci/vpci.c int vpci_ecam_mmio_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int len, · line 510

Quoted 8 lines
xen/drivers/vpci/vpci.c +1 -1
@@ -507,7 +507,7 @@ int vpci_ecam_mmio_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int len,
 
     vpci_write(sbdf, reg, min(4u, len), data);
     if ( len == 8 )
-        vpci_write(sbdf, reg + 4, 4, data >> 32);
+        vpci_write(sbdf, reg + 4, 4, (uint64_t)data >> 32);

I assume the need for this change will go away with the use of CONFIG_64BIT in the earlier patch.

xen/include/xen/pci.h line 44

Quoted 8 lines
xen/include/xen/pci.h +2 -0
@@ -40,6 +40,9 @@
 #define PCI_SBDF3(s,b,df) \
     ((pci_sbdf_t){ .sbdf = (((s) & 0xffff) << 16) | PCI_BDF2(b, df) })
 
+#define ECAM_BDF(addr)         ( ((addr) & 0x0ffff000) >> 12)
+#define ECAM_REG_OFFSET(addr)  ((addr) & 0x00000fff)

The latter is fine to be put here (i.e. FTAOD I'm fine with it staying here). For the former I even question its original placement in asm-x86/pci.h: It's not generally correct as per the PCI spec, as the bus portion of the address can be anywhere from 1 to 8 bits. And in fact there is a reason why this macro was/is used in only a single place, but not e.g. in x86'es handling of physical MCFG. It is merely an implementation choice in vPCI that the entire segment 0 has a linear address range covering all 256 buses. Hence I think this wants to move to xen/vpci.h and then perhaps also be named VPCI_ECAM_BDF().

Also, as already pointed out on a much earlier version, while the blank following the opening parenthesis was warranted in asm-x86/pci.h for alignment reasons, it is no longer warranted here. It was certainly gone in v4 and v5.

And one final nit: I don't think we commonly use full stops in patch titles. (Personally I therefore also think titles shouldn't start with a capital letter, but I know others think differently.)

Jan

#134 lore raw

Re: [PATCH v6 1/3] xen/vpci: Move ecam access functions to common code

In reply to Bertrand Marquis
Quoted 26 lines · Bertrand Marquis
Quoted 26 lines · Jan Beulich
Quoted 24 lines · Bertrand Marquis
Quoted 23 lines · Jan Beulich
Quoted 10 lines · Bertrand Marquis
xen/arch/x86/hvm/io.c +1 -1
@@ -305,7 +291,7 @@ static int vpci_portio_read(const struct hvm_io_handler *handler,
 
   reg = hvm_pci_decode_addr(cf8, addr, &sbdf);
 
-    if ( !vpci_access_allowed(reg, size) )
+    if ( !vpci_ecam_access_allowed(reg, size) )
       return X86EMUL_OKAY;
 
   *data = vpci_read(sbdf, reg, size);

xen/arch/x86/hvm/io.c static int vpci_portio_write(const struct hvm_io_handler *handler, · line 327

Quoted 8 lines
xen/arch/x86/hvm/io.c +1 -1
@@ -335,7 +321,7 @@ static int vpci_portio_write(const struct hvm_io_handler *handler,
 
   reg = hvm_pci_decode_addr(cf8, addr, &sbdf);
 
-    if ( !vpci_access_allowed(reg, size) )
+    if ( !vpci_ecam_access_allowed(reg, size) )
       return X86EMUL_OKAY;
 
   vpci_write(sbdf, reg, size, data);

Why would port I/O functions call an ECAM helper? And in how far is that helper actually ECAM-specific?

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

The function was global before.

I'm not objecting to the function being global, but to the "ecam" in its name.

Adding ecam in the name was a request from Roger.
This is just a consequence of this.

Roger - did you have in mind the uses here when asking for the addition of "ecam"?

Quoted 2 lines

One suggestion here could be to turn vpci_ecam_access_allowed into vpci_access_allowed

That's what I'm asking for.

Quoted 1 line

and maybe put this into vpci.h as a static inline ?

I'm not overly fussed here.

Quoted 50 lines
Quoted 43 lines
Quoted 38 lines
Quoted 9 lines

xen/arch/x86/hvm/io.c static int vpci_mmcfg_read(struct vcpu *v, unsigned long addr, · line 439

Quoted 7 lines
xen/arch/x86/hvm/io.c +0 -3
@@ -434,25 +420,8 @@ static int vpci_mmcfg_read(struct vcpu *v, unsigned long addr,
   reg = vpci_mmcfg_decode_addr(mmcfg, addr, &sbdf);
   read_unlock(&d->arch.hvm.mmcfg_lock);
 
-    if ( !vpci_access_allowed(reg, len) ||
-         (reg + len) > PCI_CFG_SPACE_EXP_SIZE )
-        return X86EMUL_OKAY;

While I assume this earlier behavior is the reason for ...

Yes :-)

Quoted 19 lines
Quoted 8 lines
xen/arch/x86/hvm/io.c +0 -7
-    /*
-     * According to the PCIe 3.1A specification:
-     *  - Configuration Reads and Writes must usually be DWORD or smaller
-     *    in size.
-     *  - Because Root Complex implementations are not required to support
-     *    accesses to a RCRB that cross DW boundaries [...] software
-     *    should take care not to cause the generation of such accesses

xen/arch/x86/hvm/io.c static int vpci_mmcfg_read(struct vcpu *v, unsigned long addr, · line 424

Quoted 10 lines
xen/arch/x86/hvm/io.c +2 -8
-     *    when accessing a RCRB unless the Root Complex will support the
-     *    access.
-     *  Xen however supports 8byte accesses by splitting them into two
-     *  4byte accesses.
-     */
-    *data = vpci_read(sbdf, reg, min(4u, len));
-    if ( len == 8 )
-        *data |= (uint64_t)vpci_read(sbdf, reg + 4, 4) << 32;
+    /* Ignore return code */
+    vpci_ecam_mmio_read(sbdf, reg, len, data);
Quoted 6 lines

... the commented-upon ignoring of the return value, I don't think that's a good way to deal with things anymore. Instead I think *data should be written to ~0 upon failure, unless it is intended for vpci_ecam_mmio_read() to take care of that case (in which case I'm not sure I would see why it needs to return an error indicator in the first place).

Quoted 4 lines

I am not sure in the first place why this is actually ignored and just returning a -1 value.
If an access is not right, an exception should be generated to the Guest instead.

Quoted 6 lines

No. That's also not what happens on bare metal, at least not on x86. Faults cannot be raised for reasons outside of the CPU; such errors (if these are errors in the first place) need to be dealt with differently. Signaling an error on the PCI bus would be possible, but would leave open how that's actually to be dealt with. Instead bad reads return all ones, while bad writes simply get dropped.

Quoted 5 lines

So that behaviour is kept here on x86 and I think as the function is generic it is right for it to return an error here. It is up to the caller to ignore it or not.
To make this more generic I could return 0 on success and -EACCESS, the caller would then handle it as he wants.

I think boolean is sufficient here, but I wouldn't object to errno- style return values. All I do object to is int when boolean is meant.

Quoted 57 lines
Quoted 47 lines
Quoted 44 lines
Quoted 35 lines
Quoted 35 lines
xen/drivers/vpci/vpci.c +35 -0
+int vpci_ecam_mmio_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int len,
+                         unsigned long data)
+{
+    if ( !vpci_ecam_access_allowed(reg, len) ||
+         (reg + len) > PCI_CFG_SPACE_EXP_SIZE )
+        return 0;
+
+    vpci_write(sbdf, reg, min(4u, len), data);
+    if ( len == 8 )
+        vpci_write(sbdf, reg + 4, 4, data >> 32);
+
+    return 1;
+}
+
+int vpci_ecam_mmio_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int len,
+                        unsigned long *data)
+{
+    if ( !vpci_ecam_access_allowed(reg, len) ||
+         (reg + len) > PCI_CFG_SPACE_EXP_SIZE )
+        return 0;
+
+    /*
+     * According to the PCIe 3.1A specification:
+     *  - Configuration Reads and Writes must usually be DWORD or smaller
+     *    in size.
+     *  - Because Root Complex implementations are not required to support
+     *    accesses to a RCRB that cross DW boundaries [...] software
+     *    should take care not to cause the generation of such accesses
+     *    when accessing a RCRB unless the Root Complex will support the
+     *    access.
+     *  Xen however supports 8byte accesses by splitting them into two
+     *  4byte accesses.
+     */
+    *data = vpci_read(sbdf, reg, min(4u, len));
+    if ( len == 8 )
Quoted 8 lines

xen/drivers/vpci/vpci.c void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size, · line 539

Quoted 4 lines
xen/drivers/vpci/vpci.c +4 -0
+        *data |= (uint64_t)vpci_read(sbdf, reg + 4, 4) << 32;
+
+    return 1;
+}

Why do these two functions return int/0/1 instead of bool/false/true (assuming, as per above, that them returning non- void is warranted at all)?

Quoted 2 lines

This is what the mmio handlers should return to say that an access was ok or not so the function stick to this standard.

Quoted 9 lines

Sticking to this would be okay if the functions here needed their address taken, such that they can be installed as hooks for a more general framework to invoke. The functions, however, only get called directly. Hence there's no reason to mirror what is in need of cleaning up elsewhere. I'm sure you're aware there we're in the (slow going) process of improving which types get used where. While the functions you refer to may not have undergone such cleanup yet, we generally expect new code to conform to the new model.

Quoted 2 lines

I am ok to rename those to vpci_ecam_{read/write}. Is it what you want ?

Yes, that's what I've been asking for, and I just saw Roger requesting the same. (I'm a little puzzled about the context though, as you reply looks disconnected here.)

Jan

#135 lore raw

Re: [PATCH v6 1/3] xen/vpci: Move ecam access functions to common code

Hi Jan,

Quoted 37 lines · Jan Beulich
Quoted 26 lines · Bertrand Marquis
Quoted 26 lines · Jan Beulich
Quoted 24 lines · Bertrand Marquis
Quoted 23 lines · Jan Beulich
Quoted 10 lines · Bertrand Marquis
xen/arch/x86/hvm/io.c +1 -1
@@ -305,7 +291,7 @@ static int vpci_portio_read(const struct hvm_io_handler *handler,
 
  reg = hvm_pci_decode_addr(cf8, addr, &sbdf);
 
-    if ( !vpci_access_allowed(reg, size) )
+    if ( !vpci_ecam_access_allowed(reg, size) )
      return X86EMUL_OKAY;
 
  *data = vpci_read(sbdf, reg, size);

xen/arch/x86/hvm/io.c static int vpci_portio_write(const struct hvm_io_handler *handler, · line 327

Quoted 8 lines
xen/arch/x86/hvm/io.c +1 -1
@@ -335,7 +321,7 @@ static int vpci_portio_write(const struct hvm_io_handler *handler,
 
  reg = hvm_pci_decode_addr(cf8, addr, &sbdf);
 
-    if ( !vpci_access_allowed(reg, size) )
+    if ( !vpci_ecam_access_allowed(reg, size) )
      return X86EMUL_OKAY;
 
  vpci_write(sbdf, reg, size, data);

Why would port I/O functions call an ECAM helper? And in how far is that helper actually ECAM-specific?

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

The function was global before.

I'm not objecting to the function being global, but to the "ecam" in its name.

Adding ecam in the name was a request from Roger.
This is just a consequence of this.

Quoted 7 lines

Roger - did you have in mind the uses here when asking for the addition of "ecam"?

Quoted 2 lines

One suggestion here could be to turn vpci_ecam_access_allowed into vpci_access_allowed

That's what I'm asking for.

Will do

Quoted 4 lines
Quoted 2 lines

and maybe put this into vpci.h as a static inline ?

I'm not overly fussed here.

Was just a suggestion, I am ok to just rename it and keep it where it is.

Quoted 51 lines
Quoted 51 lines
Quoted 50 lines
Quoted 39 lines
Quoted 10 lines

xen/arch/x86/hvm/io.c static int vpci_mmcfg_read(struct vcpu *v, unsigned long addr, · line 439

Quoted 8 lines
xen/arch/x86/hvm/io.c +0 -3
@@ -434,25 +420,8 @@ static int vpci_mmcfg_read(struct vcpu *v, unsigned long addr,
  reg = vpci_mmcfg_decode_addr(mmcfg, addr, &sbdf);
  read_unlock(&d->arch.hvm.mmcfg_lock);
 
-    if ( !vpci_access_allowed(reg, len) ||
-         (reg + len) > PCI_CFG_SPACE_EXP_SIZE )
-        return X86EMUL_OKAY;

While I assume this earlier behavior is the reason for ...

Yes :-)

Quoted 19 lines
Quoted 8 lines
xen/arch/x86/hvm/io.c +0 -7
-    /*
-     * According to the PCIe 3.1A specification:
-     *  - Configuration Reads and Writes must usually be DWORD or smaller
-     *    in size.
-     *  - Because Root Complex implementations are not required to support
-     *    accesses to a RCRB that cross DW boundaries [...] software
-     *    should take care not to cause the generation of such accesses

xen/arch/x86/hvm/io.c static int vpci_mmcfg_read(struct vcpu *v, unsigned long addr, · line 424

Quoted 10 lines
xen/arch/x86/hvm/io.c +2 -8
-     *    when accessing a RCRB unless the Root Complex will support the
-     *    access.
-     *  Xen however supports 8byte accesses by splitting them into two
-     *  4byte accesses.
-     */
-    *data = vpci_read(sbdf, reg, min(4u, len));
-    if ( len == 8 )
-        *data |= (uint64_t)vpci_read(sbdf, reg + 4, 4) << 32;
+    /* Ignore return code */
+    vpci_ecam_mmio_read(sbdf, reg, len, data);
Quoted 6 lines

... the commented-upon ignoring of the return value, I don't think that's a good way to deal with things anymore. Instead I think *data should be written to ~0 upon failure, unless it is intended for vpci_ecam_mmio_read() to take care of that case (in which case I'm not sure I would see why it needs to return an error indicator in the first place).

Quoted 4 lines

I am not sure in the first place why this is actually ignored and just returning a -1 value.
If an access is not right, an exception should be generated to the Guest instead.

No. That's also not what happens on bare metal, at least not on x86. Faults cannot be raised for reasons outside of the CPU; such errors (if these are errors in the first place) need to be dealt with differently. Signaling an error on the PCI bus would be possible, but would leave open how that's actually to be dealt with. Instead bad reads return all ones, while bad writes simply get dropped.

Quoted 8 lines
Quoted 5 lines

So that behaviour is kept here on x86 and I think as the function is generic it is right for it to return an error here. It is up to the caller to ignore it or not.
To make this more generic I could return 0 on success and -EACCESS, the caller would then handle it as he wants.

I think boolean is sufficient here, but I wouldn't object to errno- style return values. All I do object to is int when boolean is meant.

Boolean sounds right as there is only one error case. I will use that.

Quoted 58 lines
Quoted 58 lines
Quoted 57 lines
Quoted 45 lines
Quoted 36 lines
Quoted 36 lines
xen/drivers/vpci/vpci.c +35 -0
+int vpci_ecam_mmio_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int len,
+                         unsigned long data)
+{
+    if ( !vpci_ecam_access_allowed(reg, len) ||
+         (reg + len) > PCI_CFG_SPACE_EXP_SIZE )
+        return 0;
+
+    vpci_write(sbdf, reg, min(4u, len), data);
+    if ( len == 8 )
+        vpci_write(sbdf, reg + 4, 4, data >> 32);
+
+    return 1;
+}
+
+int vpci_ecam_mmio_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int len,
+                        unsigned long *data)
+{
+    if ( !vpci_ecam_access_allowed(reg, len) ||
+         (reg + len) > PCI_CFG_SPACE_EXP_SIZE )
+        return 0;
+
+    /*
+     * According to the PCIe 3.1A specification:
+     *  - Configuration Reads and Writes must usually be DWORD or smaller
+     *    in size.
+     *  - Because Root Complex implementations are not required to support
+     *    accesses to a RCRB that cross DW boundaries [...] software
+     *    should take care not to cause the generation of such accesses
+     *    when accessing a RCRB unless the Root Complex will support the
+     *    access.
+     *  Xen however supports 8byte accesses by splitting them into two
+     *  4byte accesses.
+     */
+    *data = vpci_read(sbdf, reg, min(4u, len));
+    if ( len == 8 )
Quoted 8 lines

xen/drivers/vpci/vpci.c void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size, · line 539

Quoted 4 lines
xen/drivers/vpci/vpci.c +4 -0
+        *data |= (uint64_t)vpci_read(sbdf, reg + 4, 4) << 32;
+
+    return 1;
+}

Why do these two functions return int/0/1 instead of bool/false/true (assuming, as per above, that them returning non- void is warranted at all)?

Quoted 2 lines

This is what the mmio handlers should return to say that an access was ok or not so the function stick to this standard.

Sticking to this would be okay if the functions here needed their address taken, such that they can be installed as hooks for a more general framework to invoke. The functions, however, only get called directly. Hence there's no reason to mirror what is in need of cleaning up elsewhere. I'm sure you're aware there we're in the (slow going) process of improving which types get used where. While the functions you refer to may not have undergone such cleanup yet, we generally expect new code to conform to the new model.

Quoted 6 lines
Quoted 2 lines

I am ok to rename those to vpci_ecam_{read/write}. Is it what you want ?

Yes, that's what I've been asking for, and I just saw Roger requesting the same. (I'm a little puzzled about the context though, as you reply looks disconnected here.)

Oups sorry.
Anyway if we agree on naming scheme and bool return type I think all your comments here are covered ?

Cheers
Bertrand

Quoted 3 lines

Jan

#136 lore raw

Re: [PATCH v6 1/3] xen/vpci: Move ecam access functions to common code

Quoted 2 lines · Bertrand Marquis

Anyway if we agree on naming scheme and bool return type I think all your comments here are covered ?

Probably, but I have to admit I've lost track ...

Jan

#160 lore raw

Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM.

In reply to Bertrand Marquis
Quoted 20 lines · Bertrand Marquis

From: Rahul Singh <[email protected]>

The existing VPCI support available for X86 is adapted for Arm. When the device is added to XEN via the hyper call “PHYSDEVOP_pci_device_add”, VPCI handler for the config space access is added to the Xen to emulate the PCI devices config space.

A MMIO trap handler for the PCI ECAM space is registered in XEN so that when guest is trying to access the PCI config space,XEN will trap the access and emulate read/write using the VPCI and not the real PCI hardware.

For Dom0less systems scan_pci_devices() would be used to discover the PCI device in XEN and VPCI handler will be added during XEN boots.

This patch is also doing some small fixes to fix compilation errors on arm32 of vpci:
- add a cast to unsigned long in print in header.c
- add a cast to uint64_t in vpci_ecam_mmio_write

Quoted 7 lines

---
Changes in v6:
- Use new vpci_ecam_ helpers for PCI access
- Do not set XEN_DOMCTL_CDF_vpci for dom0 for now (will be done in a future patch once everything is ready)

Isn't the series missing a revert of XEN_DOMCTL_CDF_vpci? I think that's what we agreed would be the way forward.

Quoted 122 lines

- rename REGISTER_OFFSET to ECAM_REG_OFFSET and move it to pci.h
- remove not needed local variables in vpci_mmio_write, the one in read has been kept to ensure proper compilation on arm32
- move call to vpci_add_handlers before iommu init to simplify exit path
- move call to pci_cleanup_msi in the out section of pci_add_device if pdev is not NULL and on error
- initialize pdev to NULL to handle properly exit path call of pci_cleanup_msi
- keep has_vpci to return false for now as CFG_vpci has been removed. Added a comment on top of the definition.
- fix compilation errors on arm32 (print in header.c, cast missing in mmio_write.
- local variable was kept in vpci_mmio_read on arm to prevent a cast error in arm32.
Change in v5:
- Add pci_cleanup_msi(pdev) incleanup part.
- Added Reviewed-by: Stefano Stabellini <[email protected]> Change in v4:
- Move addition of XEN_DOMCTL_CDF_vpci flag to separate patch Change in v3:
- Use is_pci_passthrough_enabled() in place of pci_passthrough_enabled variable
- Reject XEN_DOMCTL_CDF_vpci for x86 in arch_sanitise_domain_config()
- Remove IS_ENABLED(CONFIG_HAS_VPCI) from has_vpci()
Change in v2:
- Add new XEN_DOMCTL_CDF_vpci flag
- modify has_vpci() to include XEN_DOMCTL_CDF_vpci
- enable vpci support when pci-passthough option is enabled.
---

---
 xen/arch/arm/Makefile         |  1 +
 xen/arch/arm/domain.c         |  4 ++
 xen/arch/arm/vpci.c           | 74 +++++++++++++++++++++++++++++++++++
 xen/arch/arm/vpci.h           | 36 +++++++++++++++++
 xen/drivers/passthrough/pci.c | 18 ++++++++-
 xen/drivers/vpci/header.c     |  3 +-
 xen/drivers/vpci/vpci.c       |  2 +-
 xen/include/asm-arm/domain.h  |  1 +
 xen/include/asm-x86/pci.h     |  2 -
 xen/include/public/arch-arm.h |  7 ++++
 xen/include/xen/pci.h         |  3 ++
 11 files changed, 146 insertions(+), 5 deletions(-)
 create mode 100644 xen/arch/arm/vpci.c
 create mode 100644 xen/arch/arm/vpci.h
xen/arch/arm/Makefile +1 -0
index 64518848b2..07f634508e 100644
@@ -7,6 +7,7 @@ ifneq ($(CONFIG_NO_PLAT),y)
 obj-y += platforms/
 endif
 obj-$(CONFIG_TEE) += tee/
+obj-$(CONFIG_HAS_VPCI) += vpci.o
 
 obj-$(CONFIG_HAS_ALTERNATIVE) += alternative.o
 obj-y += bootfdt.init.o
xen/arch/arm/domain.c +4 -0
index eef0661beb..96e1b23550 100644
@@ -39,6 +39,7 @@
 #include <asm/vgic.h>
 #include <asm/vtimer.h>
 
+#include "vpci.h"
 #include "vuart.h"
 
 DEFINE_PER_CPU(struct vcpu *, curr_vcpu);
@@ -773,6 +774,9 @@ int arch_domain_create(struct domain *d,
     if ( is_hardware_domain(d) && (rc = domain_vuart_init(d)) )
         goto fail;
 
+    if ( (rc = domain_vpci_init(d)) != 0 )
+        goto fail;
+
     return 0;
 
 fail:
xen/arch/arm/vpci.c new file +38 -0
new file mode 100644
index 0000000000..7c3552b65d
@@ -0,0 +1,74 @@
+/*
+ * xen/arch/arm/vpci.c
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+#include <xen/sched.h>
+#include <xen/vpci.h>
+
+#include <asm/mmio.h>
+
+static int vpci_mmio_read(struct vcpu *v, mmio_info_t *info,
+                          register_t *r, void *p)
+{
+    pci_sbdf_t sbdf;
+    /* data is needed to prevent a pointer cast on 32bit */
+    unsigned long data = ~0ul;
+    int ret;
+
+    /* We ignore segment part and always handle segment 0 */
+    sbdf.sbdf = ECAM_BDF(info->gpa);
+
+    ret = vpci_ecam_mmio_read(sbdf, ECAM_REG_OFFSET(info->gpa),
+                              1U << info->dabt.size, &data);
+
+    *r = data;
+
+    return ret;
+}
+
+static int vpci_mmio_write(struct vcpu *v, mmio_info_t *info,

xen/arch/arm/vpci.c line 48

Quoted 10 lines
xen/arch/arm/vpci.c new file +10 -0
+                           register_t r, void *p)
+{
+    pci_sbdf_t sbdf;
+
+    /* We ignore segment part and always handle segment 0 */
+    sbdf.sbdf = ECAM_BDF(info->gpa);
+
+    return vpci_ecam_mmio_write(sbdf, ECAM_REG_OFFSET(info->gpa),
+                                1U << info->dabt.size, r);
+}

I'm not sure returning an error value here is helpful, as I'm not sure how native Arm behaves and how this error is propagated into the guest. FWIWreturning ~0 or dropping writes is what we do in x86 when vPCI is not capable of handling the access.

Quoted 87 lines
xen/arch/arm/vpci.c new file +26 -0
+
+static const struct mmio_handler_ops vpci_mmio_handler = {
+    .read  = vpci_mmio_read,
+    .write = vpci_mmio_write,
+};
+
+int domain_vpci_init(struct domain *d)
+{
+    if ( !has_vpci(d) )
+        return 0;
+
+    register_mmio_handler(d, &vpci_mmio_handler,
+                          GUEST_VPCI_ECAM_BASE, GUEST_VPCI_ECAM_SIZE, NULL);
+
+    return 0;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
+
xen/arch/arm/vpci.h new file +36 -0
new file mode 100644
index 0000000000..d8a7b0e3e8
@@ -0,0 +1,36 @@
+/*
+ * xen/arch/arm/vpci.h
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __ARCH_ARM_VPCI_H__
+#define __ARCH_ARM_VPCI_H__
+
+#ifdef CONFIG_HAS_VPCI
+int domain_vpci_init(struct domain *d);
+#else
+static inline int domain_vpci_init(struct domain *d)
+{
+    return 0;
+}
+#endif
+
+#endif /* __ARCH_ARM_VPCI_H__ */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/drivers/passthrough/pci.c +3 -1
index 3aa8c3175f..8cc529ecec 100644
@@ -658,7 +658,7 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
                    const struct pci_dev_info *info, nodeid_t node)
 {
     struct pci_seg *pseg;
-    struct pci_dev *pdev;
+    struct pci_dev *pdev = NULL;
     unsigned int slot = PCI_SLOT(devfn), func = PCI_FUNC(devfn);
     const char *pdev_type;
     int ret;
@@ -752,6 +752,19 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
 
     check_pdev(pdev);
 
+#ifdef CONFIG_ARM
+    /*

xen/drivers/passthrough/pci.c int pci_add_device(u16 seg, u8 bus, u8 devfn, · line 766

Quoted 10 lines
xen/drivers/passthrough/pci.c +10 -0
+     * On ARM PCI devices discovery will be done by Dom0. Add vpci handler when
+     * Dom0 inform XEN to add the PCI devices in XEN.
+     */
+    ret = vpci_add_handlers(pdev);
+    if ( ret )
+    {
+        printk(XENLOG_ERR "Setup of vPCI failed: %d\n", ret);
+        goto out;
+    }
+#endif

I think vpci_add_handlers should be called after checking that pdev->domain is != NULL, so I would move this chunk a bit below.

xen/drivers/passthrough/pci.c out: · line 801

Quoted 10 lines
xen/drivers/passthrough/pci.c +3 -0
+
     ret = 0;
     if ( !pdev->domain )
     {
@@ -784,6 +797,9 @@ out:
                    &PCI_SBDF(seg, bus, slot, func));
         }
     }
+    else if ( pdev )
+        pci_cleanup_msi(pdev);

I'm slightly lost at why you add this chunk, is this strictly related to the patch?

Quoted 78 lines
xen/drivers/passthrough/pci.c +0 -0
     return ret;
 }
 
xen/drivers/vpci/header.c +2 -1
index f8cd55e7c0..c5b025b88b 100644
@@ -374,7 +374,8 @@ static void bar_write(const struct pci_dev *pdev, unsigned int reg,
         if ( val != (uint32_t)(bar->addr >> (hi ? 32 : 0)) )
             gprintk(XENLOG_WARNING,
                     "%pp: ignored BAR %lu write with memory decoding enabled\n",
-                    &pdev->sbdf, bar - pdev->vpci->header.bars + hi);
+                    &pdev->sbdf,
+                    (unsigned long)(bar - pdev->vpci->header.bars + hi));
         return;
     }
 
xen/drivers/vpci/vpci.c +1 -1
index c0853176d7..2bd67fc27a 100644
@@ -507,7 +507,7 @@ int vpci_ecam_mmio_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int len,
 
     vpci_write(sbdf, reg, min(4u, len), data);
     if ( len == 8 )
-        vpci_write(sbdf, reg + 4, 4, data >> 32);
+        vpci_write(sbdf, reg + 4, 4, (uint64_t)data >> 32);
 
     return 1;
 }
xen/include/asm-arm/domain.h +1 -0
index 14e575288f..9b3647587a 100644
@@ -263,6 +263,7 @@ static inline void arch_vcpu_block(struct vcpu *v) {}
 
 #define arch_vm_assist_valid_mask(d) (1UL << VMASST_TYPE_runstate_update_flag)
 
+/* vPCI is not available on Arm */
 #define has_vpci(d)    ({ (void)(d); false; })
 
 #endif /* __ASM_DOMAIN_H__ */
xen/include/asm-x86/pci.h +0 -2
index a0df5c1279..443f25347d 100644
@@ -6,8 +6,6 @@
 #define CF8_ADDR_HI(cf8) (  ((cf8) & 0x0f000000) >> 16)
 #define CF8_ENABLED(cf8) (!!((cf8) & 0x80000000))
 
-#define ECAM_BDF(addr)   ( ((addr) & 0x0ffff000) >> 12)
-
 #define IS_SNB_GFX(id) (id == 0x01068086 || id == 0x01168086 \
                         || id == 0x01268086 || id == 0x01028086 \
                         || id == 0x01128086 || id == 0x01228086 \
xen/include/public/arch-arm.h +7 -0
index d46c61fca9..44be337dec 100644
@@ -418,6 +418,13 @@ typedef uint64_t xen_callback_t;
 #define GUEST_GICV3_GICR0_BASE     xen_mk_ullong(0x03020000) /* vCPU0..127 */
 #define GUEST_GICV3_GICR0_SIZE     xen_mk_ullong(0x01000000)
 
+/*
+ * 256 MB is reserved for VPCI configuration space based on calculation
+ * 256 buses × 32 devices × 8 functions × 4 KB = 256 MB
+ */
+#define GUEST_VPCI_ECAM_BASE    xen_mk_ullong(0x10000000)
+#define GUEST_VPCI_ECAM_SIZE    xen_mk_ullong(0x10000000)
+
 /* ACPI tables physical address */
 #define GUEST_ACPI_BASE xen_mk_ullong(0x20000000)
 #define GUEST_ACPI_SIZE xen_mk_ullong(0x02000000)
xen/include/xen/pci.h +0 -0
index 70ac25345c..db18cb7639 100644

xen/include/xen/pci.h line 43

Quoted 4 lines
xen/include/xen/pci.h +1 -0
@@ -40,6 +40,9 @@
 #define PCI_SBDF3(s,b,df) \
     ((pci_sbdf_t){ .sbdf = (((s) & 0xffff) << 16) | PCI_BDF2(b, df) })
 
+#define ECAM_BDF(addr)         ( ((addr) & 0x0ffff000) >> 12)

^ extra space?

Thanks, Roger.

#161 lore raw

Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM.

Quoted 29 lines · Roger Pau Monné
Quoted 20 lines · Bertrand Marquis

From: Rahul Singh <[email protected]>

The existing VPCI support available for X86 is adapted for Arm. When the device is added to XEN via the hyper call “PHYSDEVOP_pci_device_add”, VPCI handler for the config space access is added to the Xen to emulate the PCI devices config space.

A MMIO trap handler for the PCI ECAM space is registered in XEN so that when guest is trying to access the PCI config space,XEN will trap the access and emulate read/write using the VPCI and not the real PCI hardware.

For Dom0less systems scan_pci_devices() would be used to discover the PCI device in XEN and VPCI handler will be added during XEN boots.

This patch is also doing some small fixes to fix compilation errors on arm32 of vpci:
- add a cast to unsigned long in print in header.c
- add a cast to uint64_t in vpci_ecam_mmio_write

Quoted 7 lines

---
Changes in v6:
- Use new vpci_ecam_ helpers for PCI access
- Do not set XEN_DOMCTL_CDF_vpci for dom0 for now (will be done in a future patch once everything is ready)

Quoted 3 lines

Isn't the series missing a revert of XEN_DOMCTL_CDF_vpci? I think that's what we agreed would be the way forward.

The revert patch has already been merged.
https://xenbits.xen.org/gitweb/?p=xen.git;a=commit;h=9516d01ac3015f522528ed6dafb3f584eaa7ed2c

Quoted 339 lines
Quoted 122 lines

- rename REGISTER_OFFSET to ECAM_REG_OFFSET and move it to pci.h
- remove not needed local variables in vpci_mmio_write, the one in read has been kept to ensure proper compilation on arm32
- move call to vpci_add_handlers before iommu init to simplify exit path
- move call to pci_cleanup_msi in the out section of pci_add_device if pdev is not NULL and on error
- initialize pdev to NULL to handle properly exit path call of pci_cleanup_msi
- keep has_vpci to return false for now as CFG_vpci has been removed. Added a comment on top of the definition.
- fix compilation errors on arm32 (print in header.c, cast missing in mmio_write.
- local variable was kept in vpci_mmio_read on arm to prevent a cast error in arm32.
Change in v5:
- Add pci_cleanup_msi(pdev) incleanup part.
- Added Reviewed-by: Stefano Stabellini <[email protected]> Change in v4:
- Move addition of XEN_DOMCTL_CDF_vpci flag to separate patch Change in v3:
- Use is_pci_passthrough_enabled() in place of pci_passthrough_enabled variable
- Reject XEN_DOMCTL_CDF_vpci for x86 in arch_sanitise_domain_config()
- Remove IS_ENABLED(CONFIG_HAS_VPCI) from has_vpci()
Change in v2:
- Add new XEN_DOMCTL_CDF_vpci flag
- modify has_vpci() to include XEN_DOMCTL_CDF_vpci
- enable vpci support when pci-passthough option is enabled.
---

---
 xen/arch/arm/Makefile         |  1 +
 xen/arch/arm/domain.c         |  4 ++
 xen/arch/arm/vpci.c           | 74 +++++++++++++++++++++++++++++++++++
 xen/arch/arm/vpci.h           | 36 +++++++++++++++++
 xen/drivers/passthrough/pci.c | 18 ++++++++-
 xen/drivers/vpci/header.c     |  3 +-
 xen/drivers/vpci/vpci.c       |  2 +-
 xen/include/asm-arm/domain.h  |  1 +
 xen/include/asm-x86/pci.h     |  2 -
 xen/include/public/arch-arm.h |  7 ++++
 xen/include/xen/pci.h         |  3 ++
 11 files changed, 146 insertions(+), 5 deletions(-)
 create mode 100644 xen/arch/arm/vpci.c
 create mode 100644 xen/arch/arm/vpci.h
xen/arch/arm/Makefile +1 -0
index 64518848b2..07f634508e 100644
@@ -7,6 +7,7 @@ ifneq ($(CONFIG_NO_PLAT),y)
 obj-y += platforms/
 endif
 obj-$(CONFIG_TEE) += tee/
+obj-$(CONFIG_HAS_VPCI) += vpci.o
 
 obj-$(CONFIG_HAS_ALTERNATIVE) += alternative.o
 obj-y += bootfdt.init.o
xen/arch/arm/domain.c +4 -0
index eef0661beb..96e1b23550 100644
@@ -39,6 +39,7 @@
 #include <asm/vgic.h>
 #include <asm/vtimer.h>
 
+#include "vpci.h"
 #include "vuart.h"
 
 DEFINE_PER_CPU(struct vcpu *, curr_vcpu);
@@ -773,6 +774,9 @@ int arch_domain_create(struct domain *d,
     if ( is_hardware_domain(d) && (rc = domain_vuart_init(d)) )
         goto fail;
 
+    if ( (rc = domain_vpci_init(d)) != 0 )
+        goto fail;
+
     return 0;
 
 fail:
xen/arch/arm/vpci.c new file +38 -0
new file mode 100644
index 0000000000..7c3552b65d
@@ -0,0 +1,74 @@
+/*
+ * xen/arch/arm/vpci.c
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+#include <xen/sched.h>
+#include <xen/vpci.h>
+
+#include <asm/mmio.h>
+
+static int vpci_mmio_read(struct vcpu *v, mmio_info_t *info,
+                          register_t *r, void *p)
+{
+    pci_sbdf_t sbdf;
+    /* data is needed to prevent a pointer cast on 32bit */
+    unsigned long data = ~0ul;
+    int ret;
+
+    /* We ignore segment part and always handle segment 0 */
+    sbdf.sbdf = ECAM_BDF(info->gpa);
+
+    ret = vpci_ecam_mmio_read(sbdf, ECAM_REG_OFFSET(info->gpa),
+                              1U << info->dabt.size, &data);
+
+    *r = data;
+
+    return ret;
+}
+
+static int vpci_mmio_write(struct vcpu *v, mmio_info_t *info,

xen/arch/arm/vpci.c line 48

Quoted 10 lines
xen/arch/arm/vpci.c new file +10 -0
+                           register_t r, void *p)
+{
+    pci_sbdf_t sbdf;
+
+    /* We ignore segment part and always handle segment 0 */
+    sbdf.sbdf = ECAM_BDF(info->gpa);
+
+    return vpci_ecam_mmio_write(sbdf, ECAM_REG_OFFSET(info->gpa),
+                                1U << info->dabt.size, r);
+}

I'm not sure returning an error value here is helpful, as I'm not sure how native Arm behaves and how this error is propagated into the guest. FWIWreturning ~0 or dropping writes is what we do in x86 when vPCI is not capable of handling the access.

Quoted 87 lines
xen/arch/arm/vpci.c new file +26 -0
+
+static const struct mmio_handler_ops vpci_mmio_handler = {
+    .read  = vpci_mmio_read,
+    .write = vpci_mmio_write,
+};
+
+int domain_vpci_init(struct domain *d)
+{
+    if ( !has_vpci(d) )
+        return 0;
+
+    register_mmio_handler(d, &vpci_mmio_handler,
+                          GUEST_VPCI_ECAM_BASE, GUEST_VPCI_ECAM_SIZE, NULL);
+
+    return 0;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
+
xen/arch/arm/vpci.h new file +36 -0
new file mode 100644
index 0000000000..d8a7b0e3e8
@@ -0,0 +1,36 @@
+/*
+ * xen/arch/arm/vpci.h
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __ARCH_ARM_VPCI_H__
+#define __ARCH_ARM_VPCI_H__
+
+#ifdef CONFIG_HAS_VPCI
+int domain_vpci_init(struct domain *d);
+#else
+static inline int domain_vpci_init(struct domain *d)
+{
+    return 0;
+}
+#endif
+
+#endif /* __ARCH_ARM_VPCI_H__ */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/drivers/passthrough/pci.c +3 -1
index 3aa8c3175f..8cc529ecec 100644
@@ -658,7 +658,7 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
                    const struct pci_dev_info *info, nodeid_t node)
 {
     struct pci_seg *pseg;
-    struct pci_dev *pdev;
+    struct pci_dev *pdev = NULL;
     unsigned int slot = PCI_SLOT(devfn), func = PCI_FUNC(devfn);
     const char *pdev_type;
     int ret;
@@ -752,6 +752,19 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
 
     check_pdev(pdev);
 
+#ifdef CONFIG_ARM
+    /*

xen/drivers/passthrough/pci.c int pci_add_device(u16 seg, u8 bus, u8 devfn, · line 766

Quoted 10 lines
xen/drivers/passthrough/pci.c +10 -0
+     * On ARM PCI devices discovery will be done by Dom0. Add vpci handler when
+     * Dom0 inform XEN to add the PCI devices in XEN.
+     */
+    ret = vpci_add_handlers(pdev);
+    if ( ret )
+    {
+        printk(XENLOG_ERR "Setup of vPCI failed: %d\n", ret);
+        goto out;
+    }
+#endif

I think vpci_add_handlers should be called after checking that pdev->domain is != NULL, so I would move this chunk a bit below.

xen/drivers/passthrough/pci.c out: · line 801

Quoted 10 lines
xen/drivers/passthrough/pci.c +3 -0
+
     ret = 0;
     if ( !pdev->domain )
     {
@@ -784,6 +797,9 @@ out:
                    &PCI_SBDF(seg, bus, slot, func));
         }
     }
+    else if ( pdev )
+        pci_cleanup_msi(pdev);

I'm slightly lost at why you add this chunk, is this strictly related to the patch?

Quoted 78 lines
xen/drivers/passthrough/pci.c +0 -0
     return ret;
 }
 
xen/drivers/vpci/header.c +2 -1
index f8cd55e7c0..c5b025b88b 100644
@@ -374,7 +374,8 @@ static void bar_write(const struct pci_dev *pdev, unsigned int reg,
         if ( val != (uint32_t)(bar->addr >> (hi ? 32 : 0)) )
             gprintk(XENLOG_WARNING,
                     "%pp: ignored BAR %lu write with memory decoding enabled\n",
-                    &pdev->sbdf, bar - pdev->vpci->header.bars + hi);
+                    &pdev->sbdf,
+                    (unsigned long)(bar - pdev->vpci->header.bars + hi));
         return;
     }
 
xen/drivers/vpci/vpci.c +1 -1
index c0853176d7..2bd67fc27a 100644
@@ -507,7 +507,7 @@ int vpci_ecam_mmio_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int len,
 
     vpci_write(sbdf, reg, min(4u, len), data);
     if ( len == 8 )
-        vpci_write(sbdf, reg + 4, 4, data >> 32);
+        vpci_write(sbdf, reg + 4, 4, (uint64_t)data >> 32);
 
     return 1;
 }
xen/include/asm-arm/domain.h +1 -0
index 14e575288f..9b3647587a 100644
@@ -263,6 +263,7 @@ static inline void arch_vcpu_block(struct vcpu *v) {}
 
 #define arch_vm_assist_valid_mask(d) (1UL << VMASST_TYPE_runstate_update_flag)
 
+/* vPCI is not available on Arm */
 #define has_vpci(d)    ({ (void)(d); false; })
 
 #endif /* __ASM_DOMAIN_H__ */
xen/include/asm-x86/pci.h +0 -2
index a0df5c1279..443f25347d 100644
@@ -6,8 +6,6 @@
 #define CF8_ADDR_HI(cf8) (  ((cf8) & 0x0f000000) >> 16)
 #define CF8_ENABLED(cf8) (!!((cf8) & 0x80000000))
 
-#define ECAM_BDF(addr)   ( ((addr) & 0x0ffff000) >> 12)
-
 #define IS_SNB_GFX(id) (id == 0x01068086 || id == 0x01168086 \
                         || id == 0x01268086 || id == 0x01028086 \
                         || id == 0x01128086 || id == 0x01228086 \
xen/include/public/arch-arm.h +7 -0
index d46c61fca9..44be337dec 100644
@@ -418,6 +418,13 @@ typedef uint64_t xen_callback_t;
 #define GUEST_GICV3_GICR0_BASE     xen_mk_ullong(0x03020000) /* vCPU0..127 */
 #define GUEST_GICV3_GICR0_SIZE     xen_mk_ullong(0x01000000)
 
+/*
+ * 256 MB is reserved for VPCI configuration space based on calculation
+ * 256 buses × 32 devices × 8 functions × 4 KB = 256 MB
+ */
+#define GUEST_VPCI_ECAM_BASE    xen_mk_ullong(0x10000000)
+#define GUEST_VPCI_ECAM_SIZE    xen_mk_ullong(0x10000000)
+
 /* ACPI tables physical address */
 #define GUEST_ACPI_BASE xen_mk_ullong(0x20000000)
 #define GUEST_ACPI_SIZE xen_mk_ullong(0x02000000)
xen/include/xen/pci.h +0 -0
index 70ac25345c..db18cb7639 100644

xen/include/xen/pci.h line 43

Quoted 4 lines
xen/include/xen/pci.h +1 -0
@@ -40,6 +40,9 @@
 #define PCI_SBDF3(s,b,df) \
     ((pci_sbdf_t){ .sbdf = (((s) & 0xffff) << 16) | PCI_BDF2(b, df) })
 
+#define ECAM_BDF(addr)         ( ((addr) & 0x0ffff000) >> 12)

^ extra space?

Thanks, Roger.

#181 lore raw

Re: [PATCH v6 3/3] arm/libxl: Emulated PCI device tree node in libxl [and 1 more messages]

In reply to Michal Orzel

Hi Michal,

Quoted 9 lines · Michal Orzel

Do you agree on the following approach:
1. Modify argument of libxl__arch_domain_init_hw_description to libxl_domain_config (patch 1)
2. Modify argument of libxl__prepare_dtb to libxl_domain_config (patch 2)
3. Remove arch_arm.vpci completely and in libxl__prepare_dtb add a check (patch 3):
if (d_config->num_pcidevs)

     FDT( make_vpci_node(gc, fdt, ainfo, dom) );

+ make_vpci_node implementation

Does it sound ok for you?

This sounds ok to me.

Cheers,

Signature · 1 line
Julien Grall
#137 lore raw

Re: [PATCH v6 1/3] xen/vpci: Move ecam access functions to common code

In reply to Jan Beulich
Quoted 29 lines · Jan Beulich
Quoted 28 lines · Bertrand Marquis
Quoted 28 lines · Jan Beulich
Quoted 25 lines · Bertrand Marquis
Quoted 15 lines · Jan Beulich
Quoted 13 lines · Bertrand Marquis
xen/arch/x86/hvm/io.c +1 -1
@@ -305,7 +291,7 @@ static int vpci_portio_read(const struct hvm_io_handler *handler,
 
   reg = hvm_pci_decode_addr(cf8, addr, &sbdf);
 
-    if ( !vpci_access_allowed(reg, size) )
+    if ( !vpci_ecam_access_allowed(reg, size) )
       return X86EMUL_OKAY;
 
   *data = vpci_read(sbdf, reg, size);
@@ -335,7 +321,7 @@ static int vpci_portio_write(const struct hvm_io_handler *handler,
 
   reg = hvm_pci_decode_addr(cf8, addr, &sbdf);
 
Quoted 8 lines

xen/arch/x86/hvm/io.c static int vpci_portio_write(const struct hvm_io_handler *handler, · line 327

Quoted 5 lines
xen/arch/x86/hvm/io.c +1 -1
-    if ( !vpci_access_allowed(reg, size) )
+    if ( !vpci_ecam_access_allowed(reg, size) )
       return X86EMUL_OKAY;
 
   vpci_write(sbdf, reg, size, data);

Why would port I/O functions call an ECAM helper? And in how far is that helper actually ECAM-specific?

The function was global before.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

I'm not objecting to the function being global, but to the "ecam" in its name.

Adding ecam in the name was a request from Roger.
This is just a consequence of this.

Roger - did you have in mind the uses here when asking for the addition of "ecam"?

I didn't realize it was also used by the IO ports accessors. Strictly speaking 64bit accesses (lenght == 8) shouldn't be possible/allowed from IO ports, but that's a red herring.

Thanks, Roger.

#162 lore raw

Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM.

In reply to Roger Pau Monné

Hi Roger,

Quoted 31 lines · Roger Pau Monné
Quoted 20 lines · Bertrand Marquis

From: Rahul Singh <[email protected]>

The existing VPCI support available for X86 is adapted for Arm. When the device is added to XEN via the hyper call “PHYSDEVOP_pci_device_add”, VPCI handler for the config space access is added to the Xen to emulate the PCI devices config space.

A MMIO trap handler for the PCI ECAM space is registered in XEN so that when guest is trying to access the PCI config space,XEN will trap the access and emulate read/write using the VPCI and not the real PCI hardware.

For Dom0less systems scan_pci_devices() would be used to discover the PCI device in XEN and VPCI handler will be added during XEN boots.

This patch is also doing some small fixes to fix compilation errors on arm32 of vpci:
- add a cast to unsigned long in print in header.c
- add a cast to uint64_t in vpci_ecam_mmio_write

Quoted 7 lines

---
Changes in v6:
- Use new vpci_ecam_ helpers for PCI access
- Do not set XEN_DOMCTL_CDF_vpci for dom0 for now (will be done in a future patch once everything is ready)

Quoted 2 lines

Isn't the series missing a revert of XEN_DOMCTL_CDF_vpci? I think that's what we agreed would be the way forward.

A separate reverse patch for that has already been merged:
https://xenbits.xen.org/gitweb/?p=xen.git;a=commit;h=9516d01ac3015f522528ed6dafb3f584eaa7ed2c

Quoted 130 lines
Quoted 130 lines

- rename REGISTER_OFFSET to ECAM_REG_OFFSET and move it to pci.h
- remove not needed local variables in vpci_mmio_write, the one in read has been kept to ensure proper compilation on arm32
- move call to vpci_add_handlers before iommu init to simplify exit path
- move call to pci_cleanup_msi in the out section of pci_add_device if pdev is not NULL and on error
- initialize pdev to NULL to handle properly exit path call of pci_cleanup_msi
- keep has_vpci to return false for now as CFG_vpci has been removed. Added a comment on top of the definition.
- fix compilation errors on arm32 (print in header.c, cast missing in mmio_write.
- local variable was kept in vpci_mmio_read on arm to prevent a cast error in arm32.
Change in v5:
- Add pci_cleanup_msi(pdev) incleanup part.
- Added Reviewed-by: Stefano Stabellini <[email protected]> Change in v4:
- Move addition of XEN_DOMCTL_CDF_vpci flag to separate patch Change in v3:
- Use is_pci_passthrough_enabled() in place of pci_passthrough_enabled variable
- Reject XEN_DOMCTL_CDF_vpci for x86 in arch_sanitise_domain_config()
- Remove IS_ENABLED(CONFIG_HAS_VPCI) from has_vpci()
Change in v2:
- Add new XEN_DOMCTL_CDF_vpci flag
- modify has_vpci() to include XEN_DOMCTL_CDF_vpci
- enable vpci support when pci-passthough option is enabled.
---
---

xen/arch/arm/Makefile         |  1 +
xen/arch/arm/domain.c         |  4 ++
xen/arch/arm/vpci.c           | 74 +++++++++++++++++++++++++++++++++++
xen/arch/arm/vpci.h           | 36 +++++++++++++++++
xen/drivers/passthrough/pci.c | 18 ++++++++-
xen/drivers/vpci/header.c     |  3 +-
xen/drivers/vpci/vpci.c       |  2 +-
xen/include/asm-arm/domain.h  |  1 +
xen/include/asm-x86/pci.h     |  2 -
xen/include/public/arch-arm.h |  7 ++++
xen/include/xen/pci.h         |  3 ++
11 files changed, 146 insertions(+), 5 deletions(-)

create mode 100644 xen/arch/arm/vpci.c
create mode 100644 xen/arch/arm/vpci.h

xen/arch/arm/Makefile +0 -0
index 64518848b2..07f634508e 100644
@@ -7,6 +7,7 @@ ifneq ($(CONFIG_NO_PLAT),y)

obj-y += platforms/
endif
obj-$(CONFIG_TEE) += tee/
+obj-$(CONFIG_HAS_VPCI) += vpci.o

obj-$(CONFIG_HAS_ALTERNATIVE) += alternative.o
obj-y += bootfdt.init.o

xen/arch/arm/domain.c +4 -0
index eef0661beb..96e1b23550 100644
@@ -39,6 +39,7 @@
 #include <asm/vgic.h>
 #include <asm/vtimer.h>
 
+#include "vpci.h"
 #include "vuart.h"
 
 DEFINE_PER_CPU(struct vcpu *, curr_vcpu);
@@ -773,6 +774,9 @@ int arch_domain_create(struct domain *d,
    if ( is_hardware_domain(d) && (rc = domain_vuart_init(d)) )
        goto fail;
 
+    if ( (rc = domain_vpci_init(d)) != 0 )
+        goto fail;
+
    return 0;
 

fail:

xen/arch/arm/vpci.c new file +45 -0
new file mode 100644
index 0000000000..7c3552b65d
@@ -0,0 +1,74 @@
+/*
+ * xen/arch/arm/vpci.c
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+#include <xen/sched.h>
+#include <xen/vpci.h>
+
+#include <asm/mmio.h>
+
+static int vpci_mmio_read(struct vcpu *v, mmio_info_t *info,
+                          register_t *r, void *p)
+{
+    pci_sbdf_t sbdf;
+    /* data is needed to prevent a pointer cast on 32bit */
+    unsigned long data = ~0ul;
+    int ret;
+
+    /* We ignore segment part and always handle segment 0 */
+    sbdf.sbdf = ECAM_BDF(info->gpa);
+
+    ret = vpci_ecam_mmio_read(sbdf, ECAM_REG_OFFSET(info->gpa),
+                              1U << info->dabt.size, &data);
+
+    *r = data;
+
+    return ret;
+}
+
+static int vpci_mmio_write(struct vcpu *v, mmio_info_t *info,
+                           register_t r, void *p)
+{
+    pci_sbdf_t sbdf;
+
+    /* We ignore segment part and always handle segment 0 */
+    sbdf.sbdf = ECAM_BDF(info->gpa);
+
Quoted 8 lines

xen/arch/arm/vpci.c line 48

Quoted 3 lines
xen/arch/arm/vpci.c new file +3 -0
+    return vpci_ecam_mmio_write(sbdf, ECAM_REG_OFFSET(info->gpa),
+                                1U << info->dabt.size, r);
+}

I'm not sure returning an error value here is helpful, as I'm not sure how native Arm behaves and how this error is propagated into the guest. FWIWreturning ~0 or dropping writes is what we do in x86 when vPCI is not capable of handling the access.

Mmio handlers can take a return code on arm if something did not work so I think this is the right way to do it on arm.
Now has agreed with Jan, we will change the return type of vpci_ecam_write (also renamed) to be a boolean.

Quoted 86 lines
Quoted 86 lines
xen/arch/arm/vpci.c new file +26 -0
+
+static const struct mmio_handler_ops vpci_mmio_handler = {
+    .read  = vpci_mmio_read,
+    .write = vpci_mmio_write,
+};
+
+int domain_vpci_init(struct domain *d)
+{
+    if ( !has_vpci(d) )
+        return 0;
+
+    register_mmio_handler(d, &vpci_mmio_handler,
+                          GUEST_VPCI_ECAM_BASE, GUEST_VPCI_ECAM_SIZE, NULL);
+
+    return 0;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
+
xen/arch/arm/vpci.h new file +36 -0
new file mode 100644
index 0000000000..d8a7b0e3e8
@@ -0,0 +1,36 @@
+/*
+ * xen/arch/arm/vpci.h
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __ARCH_ARM_VPCI_H__
+#define __ARCH_ARM_VPCI_H__
+
+#ifdef CONFIG_HAS_VPCI
+int domain_vpci_init(struct domain *d);
+#else
+static inline int domain_vpci_init(struct domain *d)
+{
+    return 0;
+}
+#endif
+
+#endif /* __ARCH_ARM_VPCI_H__ */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/drivers/passthrough/pci.c +1 -1
index 3aa8c3175f..8cc529ecec 100644
@@ -658,7 +658,7 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
                   const struct pci_dev_info *info, nodeid_t node)
 {
    struct pci_seg *pseg;
-    struct pci_dev *pdev;
+    struct pci_dev *pdev = NULL;
    unsigned int slot = PCI_SLOT(devfn), func = PCI_FUNC(devfn);
    const char *pdev_type;
    int ret;
@@ -752,6 +752,19 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
 
    check_pdev(pdev);
 
Quoted 15 lines

xen/drivers/passthrough/pci.c int pci_add_device(u16 seg, u8 bus, u8 devfn, · line 766

Quoted 12 lines
xen/drivers/passthrough/pci.c +12 -0
+#ifdef CONFIG_ARM
+    /*
+     * On ARM PCI devices discovery will be done by Dom0. Add vpci handler when
+     * Dom0 inform XEN to add the PCI devices in XEN.
+     */
+    ret = vpci_add_handlers(pdev);
+    if ( ret )
+    {
+        printk(XENLOG_ERR "Setup of vPCI failed: %d\n", ret);
+        goto out;
+    }
+#endif

I think vpci_add_handlers should be called after checking that pdev->domain is != NULL, so I would move this chunk a bit below.

On arm this would prevent the dom0less use case or to have the PCI bus enumerated from an other domain.
@oleksandr: can you comment on this one, you might have a better answer than me on this ?

Quoted 14 lines

xen/drivers/passthrough/pci.c out: · line 801

Quoted 11 lines
xen/drivers/passthrough/pci.c +3 -0
+
    ret = 0;
    if ( !pdev->domain )
    {
@@ -784,6 +797,9 @@ out:
                   &PCI_SBDF(seg, bus, slot, func));
        }
    }
+    else if ( pdev )
+        pci_cleanup_msi(pdev);

I'm slightly lost at why you add this chunk, is this strictly related to the patch?

This was discussed a lot in previous version of the patch and requested by Stefano. The idea here is that as soon as handlers are added some bits might be modified in the PCI config space leading possibly to msi interrupts. So it is safer to cleanup on the error path. For references please see discussion on v4 and v5 where this was actually added (to much references as the discussion was long so here [1] and [2] are the patchwork thread).

[1] https://patchwork.kernel.org/project/xen-devel/patch/9bdca2cda5d2e83f94dc2423e55714273539760a.1633540842.git.rahul.singh@arm.com/
[2] https://patchwork.kernel.org/project/xen-devel/patch/f093de681c2560a7196895bcd666ef8840885c1d.1633340795.git.rahul.singh@arm.com/

Regards
Bertrand

#141 lore raw

Re: [PATCH v6 1/3] xen/vpci: Move ecam access functions to common code

In reply to Bertrand Marquis
Quoted 32 lines · Bertrand Marquis
Quoted 23 lines · Roger Pau Monné
Quoted 21 lines · Bertrand Marquis
xen/arch/x86/hvm/io.c +0 -17
@@ -434,25 +420,8 @@ static int vpci_mmcfg_read(struct vcpu *v, unsigned long addr,
    reg = vpci_mmcfg_decode_addr(mmcfg, addr, &sbdf);
    read_unlock(&d->arch.hvm.mmcfg_lock);
 
-    if ( !vpci_access_allowed(reg, len) ||
-         (reg + len) > PCI_CFG_SPACE_EXP_SIZE )
-        return X86EMUL_OKAY;
-
-    /*
-     * According to the PCIe 3.1A specification:
-     *  - Configuration Reads and Writes must usually be DWORD or smaller
-     *    in size.
-     *  - Because Root Complex implementations are not required to support
-     *    accesses to a RCRB that cross DW boundaries [...] software
-     *    should take care not to cause the generation of such accesses
-     *    when accessing a RCRB unless the Root Complex will support the
-     *    access.
-     *  Xen however supports 8byte accesses by splitting them into two
-     *  4byte accesses.
-     */
-    *data = vpci_read(sbdf, reg, min(4u, len));
Quoted 8 lines

xen/arch/x86/hvm/io.c static int vpci_mmcfg_read(struct vcpu *v, unsigned long addr, · line 424

Quoted 4 lines
xen/arch/x86/hvm/io.c +2 -2
-    if ( len == 8 )
-        *data |= (uint64_t)vpci_read(sbdf, reg + 4, 4) << 32;
+    /* Ignore return code */
+    vpci_ecam_mmio_read(sbdf, reg, len, data);

I think it would be better for vpci_ecam_mmio_read to just return the read value, or ~0 in case of error, at least that interface would be simpler and suitable for x86.

Quoted 2 lines

I am not quite sure on this as on absolute to read ~0 is possible so the caller cannot distinguish between properly reading ~0 or an access allowed error.

How do you report an access allowed error on Arm for the PCI config space?

At least on x86 I don't think we currently have a way to propagate such errors, neither a plan to do so that I'm aware.

Thanks, Roger.

#147 lore raw

Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM.

In reply to Jan Beulich

Jan Beulich writes ("Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM."):

Quoted 8 lines · Stefano Stabellini
Quoted 2 lines

Replacing the 3 characters with 'x' solves the problem.
https://gitlab.com/xen-project/people/sstabellini/xen/-/pipelines/388721262

Interesting. I thought we permit UTF-8 in the sources; see e.g. tools/tests/x86_emulator/simd-sha.c using Σ. Is mkheader.py in need of adjustment? Of course I can see that right now the easiest is to use ASCII x, but I think it was deliberate to use × here.

I think in general we should permit UTF-8 in sources.

But:

Quoted 4 lines

Then again, with the goal of the public headers being usable with pretty old compilers as well (C89 being the assumed baseline), excluding them from the permission to use UTF-8 may also be quite reasonable.

This is a good reason to do otherwise for the public headers. Maybe this should be documented in CODING_STYLE (can be done after feature freeze obviously).

That python has such botched unicode handling is a bug of course but we could decide we think it's a feature :-). In which case maybe mkheaders should be adjusted to explicitly set IO to ascii so that this will fail in local builds too.

Anyway, for now we need to replace the UTF-8 in this patch.

Thanks,
Ian.

#182 lore raw

Re: [PATCH v6 3/3] arm/libxl: Emulated PCI device tree node in libxl [and 1 more messages]

In reply to Julien Grall

Julien Grall writes ("Re: [PATCH v6 3/3] arm/libxl: Emulated PCI device tree node in libxl [and 1 more messages]"):

Quoted 1 line · Ian Jackson

...

Quoted 8 lines
Quoted 3 lines

That is, as I understand it on ARM vpci should be enabled if passthrough is enabled and not otherwise. That is precisely what the variable c_info->passthrough is.

On Arm, c_info->passthrough is also set when assigning platform devives (e.g. a non-PCI network card). At least for now, we don't want to create a node for vCPI in the Device-Tree because we don't enable the emulation. So...

Oh.

Quoted 5 lines
Quoted 3 lines

3. Now you can use d_config->c_info.passthrough to gate the addition of the additional stuff to the DT. Ie, that is a respin of this patch 3/3.

... we will need to check d_config->num_pcidevs for the time being.

OK.

Can you leave a comment somewhere (near where c_info.passthrough is set) pointing to this use of num_pcidevs ? That might save someone some future confusion.

Thanks,
Ian.

#150 lore raw

Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM.

In reply to Jan Beulich

Hi Jan,

Quoted 25 lines · Jan Beulich
Quoted 9 lines · Bertrand Marquis
xen/drivers/passthrough/pci.c +5 -0
@@ -752,6 +752,19 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
 
    check_pdev(pdev);
 
+#ifdef CONFIG_ARM
+    /*
+     * On ARM PCI devices discovery will be done by Dom0. Add vpci handler when
+     * Dom0 inform XEN to add the PCI devices in XEN.
+     */

xen/drivers/passthrough/pci.c int pci_add_device(u16 seg, u8 bus, u8 devfn, · line 769

Quoted 10 lines
xen/drivers/passthrough/pci.c +8 -0
+    ret = vpci_add_handlers(pdev);
+    if ( ret )
+    {
+        printk(XENLOG_ERR "Setup of vPCI failed: %d\n", ret);
+        goto out;
+    }
+#endif
+
    ret = 0;
    if ( !pdev->domain )

Elsewhere I did point out that you need to be careful wrt pdev->domain being NULL. As the code in context clearly documents, you're now

Quoted 8 lines

adding handlers before that field was set. In comments to a prior version I did already suggest to consider placing the new code inside the if() in question (albeit at the time this was mainly to make clear that error cleanup may not fiddle with things not put in place by the iommu_enable_device() alternative path). This would have the further benefit of making is crystal clear that here only handlers for Dom0 would get put in place (and hence their installing for DomU-s would need to be looked for elsewhere).

I asked Oleksandr for confirmation here but on arm there will be 2 other use cases:
- PCI own by a DomD so device enumeration done from there
- dom0less with devices detection done inside Xen

Quoted 14 lines

xen/drivers/passthrough/pci.c out: · line 801

Quoted 7 lines
xen/drivers/passthrough/pci.c +2 -0
@@ -784,6 +797,9 @@ out:
                   &PCI_SBDF(seg, bus, slot, func));
        }
    }
+    else if ( pdev )
+        pci_cleanup_msi(pdev);

Have you thoroughly checked that this call is benign on x86? There's no wording to that effect in the description afaics, and I can't easily convince myself that it would be correct when the iommu_enable_device() path was taken. (I'm also not going to exclude that it should have been there even prior to your work, albeit then adding this would want to be a separate bugfix patch.)

This was not in the original serie and requested by Stefano. I must admit I am not completely sure on the details here so I am really ok to remove this but this would go against what was requested on the previous versions (4 and 5).

Quoted 17 lines

xen/drivers/vpci/header.c static void bar_write(const struct pci_dev *pdev, unsigned int reg, · line 378

Quoted 10 lines
xen/drivers/vpci/header.c +2 -1
@@ -374,7 +374,8 @@ static void bar_write(const struct pci_dev *pdev, unsigned int reg,
        if ( val != (uint32_t)(bar->addr >> (hi ? 32 : 0)) )
            gprintk(XENLOG_WARNING,
                    "%pp: ignored BAR %lu write with memory decoding enabled\n",
-                    &pdev->sbdf, bar - pdev->vpci->header.bars + hi);
+                    &pdev->sbdf,
+                    (unsigned long)(bar - pdev->vpci->header.bars + hi));

This looks like an entirely unrelated change which I'm afraid I don't even understand why it needs making. The description says this is for Arm32, but it remains unclear what the compilation error would have been. My best guess is that perhaps you really mean to change the format specifier to %zu (really this should be %td, but our vsprintf() doesn't support 't' for whatever reason).

Compilation error is about an invalid %lu print for an unsigned int value when compiled for arm32.

I will use zu instead in the next version and remove the cast.

Quoted 2 lines

Please recall that we try to avoid casts where possible.

Sure I will.

Quoted 5 lines
Quoted 5 lines
xen/drivers/vpci/vpci.c +0 -0
@@ -507,7 +507,7 @@ int vpci_ecam_mmio_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int len,
 
Quoted 7 lines

xen/drivers/vpci/vpci.c int vpci_ecam_mmio_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int len, · line 510

Quoted 4 lines
xen/drivers/vpci/vpci.c +1 -1
    vpci_write(sbdf, reg, min(4u, len), data);
    if ( len == 8 )
-        vpci_write(sbdf, reg + 4, 4, data >> 32);
+        vpci_write(sbdf, reg + 4, 4, (uint64_t)data >> 32);

I assume the need for this change will go away with the use of CONFIG_64BIT in the earlier patch.

Yes

Quoted 7 lines
Quoted 7 lines
xen/include/xen/pci.h +0 -0
@@ -40,6 +40,9 @@
 #define PCI_SBDF3(s,b,df) \
    ((pci_sbdf_t){ .sbdf = (((s) & 0xffff) << 16) | PCI_BDF2(b, df) })
 
Quoted 13 lines

xen/include/xen/pci.h line 44

Quoted 2 lines
xen/include/xen/pci.h +2 -0
+#define ECAM_BDF(addr)         ( ((addr) & 0x0ffff000) >> 12)
+#define ECAM_REG_OFFSET(addr)  ((addr) & 0x00000fff)

The latter is fine to be put here (i.e. FTAOD I'm fine with it staying here). For the former I even question its original placement in asm-x86/pci.h: It's not generally correct as per the PCI spec, as the bus portion of the address can be anywhere from 1 to 8 bits. And in fact there is a reason why this macro was/is used in only a single place, but not e.g. in x86'es handling of physical MCFG. It is merely an implementation choice in vPCI that the entire segment 0 has a linear address range covering all 256 buses. Hence I think this wants to move to xen/vpci.h and then perhaps also be named VPCI_ECAM_BDF().

On previous version it was request to renamed this to ECAM and agreed to put is here. Now you want me to rename it to VPCI and move it again. I would like to have a confirmation that this is ok and the final move if possible.

@Roger can you confirm this is what is wanted ?

Also if I have to do this I will do the same for REG_OFFSET of course.

Quoted 5 lines

Also, as already pointed out on a much earlier version, while the blank following the opening parenthesis was warranted in asm-x86/pci.h for alignment reasons, it is no longer warranted here. It was certainly gone in v4 and v5.

Yes my mistake during the process I did a copy and paste from the original one and did not modify. I will fix that.

Quoted 4 lines

And one final nit: I don't think we commonly use full stops in patch titles. (Personally I therefore also think titles shouldn't start with a capital letter, but I know others think differently.)

Ok I will fix the patch name.

Cheers
Bertrand

Quoted 3 lines

Jan

#148 lore raw

Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM.

In reply to Stefano Stabellini

Hi Stefano,

Quoted 365 lines · Stefano Stabellini
Quoted 12 lines · Bertrand Marquis

From: Rahul Singh <[email protected]>

The existing VPCI support available for X86 is adapted for Arm. When the device is added to XEN via the hyper call “PHYSDEVOP_pci_device_add”, VPCI handler for the config space access is added to the Xen to emulate the PCI devices config space.

A MMIO trap handler for the PCI ECAM space is registered in XEN so that when guest is trying to access the PCI config space,XEN will trap the access and emulate read/write using the VPCI and not the real PCI hardware.

Quoted 7 lines

For Dom0less systems scan_pci_devices() would be used to discover the PCI device in XEN and VPCI handler will be added during XEN boots.

This patch is also doing some small fixes to fix compilation errors on arm32 of vpci:
- add a cast to unsigned long in print in header.c
- add a cast to uint64_t in vpci_ecam_mmio_write

Thank you for these! Strictly speaking they are not required now but they are welcome. I would also be OK if they were removed from this patch but it is fine to have them in here.

There is an issues with this patch, see below at the bottom

Quoted 308 lines

---
Changes in v6:
- Use new vpci_ecam_ helpers for PCI access
- Do not set XEN_DOMCTL_CDF_vpci for dom0 for now (will be done in a future patch once everything is ready)
- rename REGISTER_OFFSET to ECAM_REG_OFFSET and move it to pci.h
- remove not needed local variables in vpci_mmio_write, the one in read has been kept to ensure proper compilation on arm32
- move call to vpci_add_handlers before iommu init to simplify exit path
- move call to pci_cleanup_msi in the out section of pci_add_device if pdev is not NULL and on error
- initialize pdev to NULL to handle properly exit path call of pci_cleanup_msi
- keep has_vpci to return false for now as CFG_vpci has been removed. Added a comment on top of the definition.
- fix compilation errors on arm32 (print in header.c, cast missing in mmio_write.
- local variable was kept in vpci_mmio_read on arm to prevent a cast error in arm32.
Change in v5:
- Add pci_cleanup_msi(pdev) incleanup part.
- Added Reviewed-by: Stefano Stabellini <[email protected]> Change in v4:
- Move addition of XEN_DOMCTL_CDF_vpci flag to separate patch Change in v3:
- Use is_pci_passthrough_enabled() in place of pci_passthrough_enabled variable
- Reject XEN_DOMCTL_CDF_vpci for x86 in arch_sanitise_domain_config()
- Remove IS_ENABLED(CONFIG_HAS_VPCI) from has_vpci()
Change in v2:
- Add new XEN_DOMCTL_CDF_vpci flag
- modify has_vpci() to include XEN_DOMCTL_CDF_vpci
- enable vpci support when pci-passthough option is enabled.
---
---

xen/arch/arm/Makefile         |  1 +
xen/arch/arm/domain.c         |  4 ++
xen/arch/arm/vpci.c           | 74 +++++++++++++++++++++++++++++++++++
xen/arch/arm/vpci.h           | 36 +++++++++++++++++
xen/drivers/passthrough/pci.c | 18 ++++++++-
xen/drivers/vpci/header.c     |  3 +-
xen/drivers/vpci/vpci.c       |  2 +-
xen/include/asm-arm/domain.h  |  1 +
xen/include/asm-x86/pci.h     |  2 -
xen/include/public/arch-arm.h |  7 ++++
xen/include/xen/pci.h         |  3 ++
11 files changed, 146 insertions(+), 5 deletions(-)

create mode 100644 xen/arch/arm/vpci.c
create mode 100644 xen/arch/arm/vpci.h

xen/arch/arm/Makefile +0 -0
index 64518848b2..07f634508e 100644
@@ -7,6 +7,7 @@ ifneq ($(CONFIG_NO_PLAT),y)

obj-y += platforms/
endif
obj-$(CONFIG_TEE) += tee/
+obj-$(CONFIG_HAS_VPCI) += vpci.o

obj-$(CONFIG_HAS_ALTERNATIVE) += alternative.o
obj-y += bootfdt.init.o

xen/arch/arm/domain.c +4 -0
index eef0661beb..96e1b23550 100644
@@ -39,6 +39,7 @@
 #include <asm/vgic.h>
 #include <asm/vtimer.h>
 
+#include "vpci.h"
 #include "vuart.h"
 
 DEFINE_PER_CPU(struct vcpu *, curr_vcpu);
@@ -773,6 +774,9 @@ int arch_domain_create(struct domain *d,
    if ( is_hardware_domain(d) && (rc = domain_vuart_init(d)) )
        goto fail;
 
+    if ( (rc = domain_vpci_init(d)) != 0 )
+        goto fail;
+
    return 0;
 

fail:

xen/arch/arm/vpci.c new file +74 -0
new file mode 100644
index 0000000000..7c3552b65d
@@ -0,0 +1,74 @@
+/*
+ * xen/arch/arm/vpci.c
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+#include <xen/sched.h>
+#include <xen/vpci.h>
+
+#include <asm/mmio.h>
+
+static int vpci_mmio_read(struct vcpu *v, mmio_info_t *info,
+                          register_t *r, void *p)
+{
+    pci_sbdf_t sbdf;
+    /* data is needed to prevent a pointer cast on 32bit */
+    unsigned long data = ~0ul;
+    int ret;
+
+    /* We ignore segment part and always handle segment 0 */
+    sbdf.sbdf = ECAM_BDF(info->gpa);
+
+    ret = vpci_ecam_mmio_read(sbdf, ECAM_REG_OFFSET(info->gpa),
+                              1U << info->dabt.size, &data);
+
+    *r = data;
+
+    return ret;
+}
+
+static int vpci_mmio_write(struct vcpu *v, mmio_info_t *info,
+                           register_t r, void *p)
+{
+    pci_sbdf_t sbdf;
+
+    /* We ignore segment part and always handle segment 0 */
+    sbdf.sbdf = ECAM_BDF(info->gpa);
+
+    return vpci_ecam_mmio_write(sbdf, ECAM_REG_OFFSET(info->gpa),
+                                1U << info->dabt.size, r);
+}
+
+static const struct mmio_handler_ops vpci_mmio_handler = {
+    .read  = vpci_mmio_read,
+    .write = vpci_mmio_write,
+};
+
+int domain_vpci_init(struct domain *d)
+{
+    if ( !has_vpci(d) )
+        return 0;
+
+    register_mmio_handler(d, &vpci_mmio_handler,
+                          GUEST_VPCI_ECAM_BASE, GUEST_VPCI_ECAM_SIZE, NULL);
+
+    return 0;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
+
xen/arch/arm/vpci.h new file +36 -0
new file mode 100644
index 0000000000..d8a7b0e3e8
@@ -0,0 +1,36 @@
+/*
+ * xen/arch/arm/vpci.h
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __ARCH_ARM_VPCI_H__
+#define __ARCH_ARM_VPCI_H__
+
+#ifdef CONFIG_HAS_VPCI
+int domain_vpci_init(struct domain *d);
+#else
+static inline int domain_vpci_init(struct domain *d)
+{
+    return 0;
+}
+#endif
+
+#endif /* __ARCH_ARM_VPCI_H__ */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/drivers/passthrough/pci.c +17 -1
index 3aa8c3175f..8cc529ecec 100644
@@ -658,7 +658,7 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
                   const struct pci_dev_info *info, nodeid_t node)
 {
    struct pci_seg *pseg;
-    struct pci_dev *pdev;
+    struct pci_dev *pdev = NULL;
    unsigned int slot = PCI_SLOT(devfn), func = PCI_FUNC(devfn);
    const char *pdev_type;
    int ret;
@@ -752,6 +752,19 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
 
    check_pdev(pdev);
 
+#ifdef CONFIG_ARM
+    /*
+     * On ARM PCI devices discovery will be done by Dom0. Add vpci handler when
+     * Dom0 inform XEN to add the PCI devices in XEN.
+     */
+    ret = vpci_add_handlers(pdev);
+    if ( ret )
+    {
+        printk(XENLOG_ERR "Setup of vPCI failed: %d\n", ret);
+        goto out;
+    }
+#endif
+
    ret = 0;
    if ( !pdev->domain )
    {
@@ -784,6 +797,9 @@ out:
                   &PCI_SBDF(seg, bus, slot, func));
        }
    }
+    else if ( pdev )
+        pci_cleanup_msi(pdev);
+
    return ret;
 }
 
xen/drivers/vpci/header.c +2 -1
index f8cd55e7c0..c5b025b88b 100644
@@ -374,7 +374,8 @@ static void bar_write(const struct pci_dev *pdev, unsigned int reg,
        if ( val != (uint32_t)(bar->addr >> (hi ? 32 : 0)) )
            gprintk(XENLOG_WARNING,
                    "%pp: ignored BAR %lu write with memory decoding enabled\n",
-                    &pdev->sbdf, bar - pdev->vpci->header.bars + hi);
+                    &pdev->sbdf,
+                    (unsigned long)(bar - pdev->vpci->header.bars + hi));
        return;
    }
 
xen/drivers/vpci/vpci.c +1 -1
index c0853176d7..2bd67fc27a 100644
@@ -507,7 +507,7 @@ int vpci_ecam_mmio_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int len,
 
    vpci_write(sbdf, reg, min(4u, len), data);
    if ( len == 8 )
-        vpci_write(sbdf, reg + 4, 4, data >> 32);
+        vpci_write(sbdf, reg + 4, 4, (uint64_t)data >> 32);
 
    return 1;
 }
xen/include/asm-arm/domain.h +1 -0
index 14e575288f..9b3647587a 100644
@@ -263,6 +263,7 @@ static inline void arch_vcpu_block(struct vcpu *v) {}
 
 #define arch_vm_assist_valid_mask(d) (1UL << VMASST_TYPE_runstate_update_flag)
 
+/* vPCI is not available on Arm */
 #define has_vpci(d)    ({ (void)(d); false; })
 
 #endif /* __ASM_DOMAIN_H__ */
xen/include/asm-x86/pci.h +0 -2
index a0df5c1279..443f25347d 100644
@@ -6,8 +6,6 @@
 #define CF8_ADDR_HI(cf8) (  ((cf8) & 0x0f000000) >> 16)
 #define CF8_ENABLED(cf8) (!!((cf8) & 0x80000000))
 
-#define ECAM_BDF(addr)   ( ((addr) & 0x0ffff000) >> 12)
-
 #define IS_SNB_GFX(id) (id == 0x01068086 || id == 0x01168086 \
                        || id == 0x01268086 || id == 0x01028086 \
                        || id == 0x01128086 || id == 0x01228086 \
xen/include/public/arch-arm.h +0 -0
index d46c61fca9..44be337dec 100644

xen/include/public/arch-arm.h typedef uint64_t xen_callback_t; · line 424

Quoted 7 lines
xen/include/public/arch-arm.h +4 -0
@@ -418,6 +418,13 @@ typedef uint64_t xen_callback_t;
 #define GUEST_GICV3_GICR0_BASE     xen_mk_ullong(0x03020000) /* vCPU0..127 */
 #define GUEST_GICV3_GICR0_SIZE     xen_mk_ullong(0x01000000)
 
+/*
+ * 256 MB is reserved for VPCI configuration space based on calculation
+ * 256 buses × 32 devices × 8 functions × 4 KB = 256 MB
+ */

Somehow 3 non-ascii characters sneaked into this patch. The 'x' are not 'x' but are 0xc3 and cause the following errors in a few gitlab-ci builds:

python3 mkheader.py arm32 arm32.h.tmp /builds/xen-project/people/sstabellini/xen/tools/include/xen-foreign/../../../xen/include/public/arch-arm.h /builds/xen-project/people/sstabellini/xen/tools/include/xen-foreign/../../../xen/include/public/xen.h Traceback (most recent call last):
File "mkheader.py", line 120, in <module>
input += f.read();
File "/usr/lib/python3.5/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 14641: ordinal not in range(128)
Makefile:28: recipe for target 'arm32.h' failed
make[2]: *** [arm32.h] Error 1

Full logs here:
https://gitlab.com/xen-project/people/sstabellini/xen/-/pipelines/38855078
https://gitlab.com/xen-project/people/sstabellini/xen/-/jobs/16810108756

Quoted 7 lines

Replacing the 3 characters with 'x' solves the problem.
https://gitlab.com/xen-project/people/sstabellini/xen/-/pipelines/388721262

With the three 'x' changed to ascii:

I will fix that but sadly seeing all other changes to be done I do not think I can keep your R-b.

Thanks
Bertrand

Quoted 25 lines
Quoted 25 lines
xen/include/public/arch-arm.h +3 -0
+#define GUEST_VPCI_ECAM_BASE    xen_mk_ullong(0x10000000)
+#define GUEST_VPCI_ECAM_SIZE    xen_mk_ullong(0x10000000)
+
 /* ACPI tables physical address */
 #define GUEST_ACPI_BASE xen_mk_ullong(0x20000000)
 #define GUEST_ACPI_SIZE xen_mk_ullong(0x02000000)
xen/include/xen/pci.h +3 -0
index 70ac25345c..db18cb7639 100644
@@ -40,6 +40,9 @@
 #define PCI_SBDF3(s,b,df) \
    ((pci_sbdf_t){ .sbdf = (((s) & 0xffff) << 16) | PCI_BDF2(b, df) })
 
+#define ECAM_BDF(addr)         ( ((addr) & 0x0ffff000) >> 12)
+#define ECAM_REG_OFFSET(addr)  ((addr) & 0x00000fff)
+
 typedef union {
    uint32_t sbdf;
    struct {
Signature · 1 line
2.25.1
#142 lore raw

Re: [PATCH v6 1/3] xen/vpci: Move ecam access functions to common code

In reply to Roger Pau Monné

Hi Roger,

Quoted 35 lines · Roger Pau Monné
Quoted 32 lines · Bertrand Marquis
Quoted 31 lines · Roger Pau Monné
Quoted 15 lines · Bertrand Marquis
xen/arch/x86/hvm/io.c +0 -11
@@ -434,25 +420,8 @@ static int vpci_mmcfg_read(struct vcpu *v, unsigned long addr,
   reg = vpci_mmcfg_decode_addr(mmcfg, addr, &sbdf);
   read_unlock(&d->arch.hvm.mmcfg_lock);
 
-    if ( !vpci_access_allowed(reg, len) ||
-         (reg + len) > PCI_CFG_SPACE_EXP_SIZE )
-        return X86EMUL_OKAY;
-
-    /*
-     * According to the PCIe 3.1A specification:
-     *  - Configuration Reads and Writes must usually be DWORD or smaller
-     *    in size.
-     *  - Because Root Complex implementations are not required to support
-     *    accesses to a RCRB that cross DW boundaries [...] software
-     *    should take care not to cause the generation of such accesses

xen/arch/x86/hvm/io.c static int vpci_mmcfg_read(struct vcpu *v, unsigned long addr, · line 424

Quoted 10 lines
xen/arch/x86/hvm/io.c +2 -8
-     *    when accessing a RCRB unless the Root Complex will support the
-     *    access.
-     *  Xen however supports 8byte accesses by splitting them into two
-     *  4byte accesses.
-     */
-    *data = vpci_read(sbdf, reg, min(4u, len));
-    if ( len == 8 )
-        *data |= (uint64_t)vpci_read(sbdf, reg + 4, 4) << 32;
+    /* Ignore return code */
+    vpci_ecam_mmio_read(sbdf, reg, len, data);

I think it would be better for vpci_ecam_mmio_read to just return the read value, or ~0 in case of error, at least that interface would be simpler and suitable for x86.

Quoted 5 lines
Quoted 2 lines

I am not quite sure on this as on absolute to read ~0 is possible so the caller cannot distinguish between properly reading ~0 or an access allowed error.

How do you report an access allowed error on Arm for the PCI config space?

This does not go that far. In the current case this is actually an unaligned access to mmio which will trigger and exception before reaching the PCI config space.

Quoted 3 lines

At least on x86 I don't think we currently have a way to propagate such errors, neither a plan to do so that I'm aware.

The behaviour on x86 will not change as we will ignore this.

Regards
Bertrand

Quoted 2 lines

Thanks, Roger.

#163 lore raw

Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM.

In reply to Bertrand Marquis
Quoted 289 lines · Bertrand Marquis

Hi Roger,

Quoted 31 lines · Roger Pau Monné
Quoted 20 lines · Bertrand Marquis

From: Rahul Singh <[email protected]>

The existing VPCI support available for X86 is adapted for Arm. When the device is added to XEN via the hyper call “PHYSDEVOP_pci_device_add”, VPCI handler for the config space access is added to the Xen to emulate the PCI devices config space.

A MMIO trap handler for the PCI ECAM space is registered in XEN so that when guest is trying to access the PCI config space,XEN will trap the access and emulate read/write using the VPCI and not the real PCI hardware.

For Dom0less systems scan_pci_devices() would be used to discover the PCI device in XEN and VPCI handler will be added during XEN boots.

This patch is also doing some small fixes to fix compilation errors on arm32 of vpci:
- add a cast to unsigned long in print in header.c
- add a cast to uint64_t in vpci_ecam_mmio_write

Quoted 7 lines

---
Changes in v6:
- Use new vpci_ecam_ helpers for PCI access
- Do not set XEN_DOMCTL_CDF_vpci for dom0 for now (will be done in a future patch once everything is ready)

Quoted 2 lines

Isn't the series missing a revert of XEN_DOMCTL_CDF_vpci? I think that's what we agreed would be the way forward.

A separate reverse patch for that has already been merged:
https://xenbits.xen.org/gitweb/?p=xen.git;a=commit;h=9516d01ac3015f522528ed6dafb3f584eaa7ed2c

Quoted 130 lines
Quoted 130 lines

- rename REGISTER_OFFSET to ECAM_REG_OFFSET and move it to pci.h
- remove not needed local variables in vpci_mmio_write, the one in read has been kept to ensure proper compilation on arm32
- move call to vpci_add_handlers before iommu init to simplify exit path
- move call to pci_cleanup_msi in the out section of pci_add_device if pdev is not NULL and on error
- initialize pdev to NULL to handle properly exit path call of pci_cleanup_msi
- keep has_vpci to return false for now as CFG_vpci has been removed. Added a comment on top of the definition.
- fix compilation errors on arm32 (print in header.c, cast missing in mmio_write.
- local variable was kept in vpci_mmio_read on arm to prevent a cast error in arm32.
Change in v5:
- Add pci_cleanup_msi(pdev) incleanup part.
- Added Reviewed-by: Stefano Stabellini <[email protected]> Change in v4:
- Move addition of XEN_DOMCTL_CDF_vpci flag to separate patch Change in v3:
- Use is_pci_passthrough_enabled() in place of pci_passthrough_enabled variable
- Reject XEN_DOMCTL_CDF_vpci for x86 in arch_sanitise_domain_config()
- Remove IS_ENABLED(CONFIG_HAS_VPCI) from has_vpci()
Change in v2:
- Add new XEN_DOMCTL_CDF_vpci flag
- modify has_vpci() to include XEN_DOMCTL_CDF_vpci
- enable vpci support when pci-passthough option is enabled.
---
---

xen/arch/arm/Makefile         |  1 +
xen/arch/arm/domain.c         |  4 ++
xen/arch/arm/vpci.c           | 74 +++++++++++++++++++++++++++++++++++
xen/arch/arm/vpci.h           | 36 +++++++++++++++++
xen/drivers/passthrough/pci.c | 18 ++++++++-
xen/drivers/vpci/header.c     |  3 +-
xen/drivers/vpci/vpci.c       |  2 +-
xen/include/asm-arm/domain.h  |  1 +
xen/include/asm-x86/pci.h     |  2 -
xen/include/public/arch-arm.h |  7 ++++
xen/include/xen/pci.h         |  3 ++
11 files changed, 146 insertions(+), 5 deletions(-)

create mode 100644 xen/arch/arm/vpci.c
create mode 100644 xen/arch/arm/vpci.h

xen/arch/arm/Makefile +0 -0
index 64518848b2..07f634508e 100644
@@ -7,6 +7,7 @@ ifneq ($(CONFIG_NO_PLAT),y)

obj-y += platforms/
endif
obj-$(CONFIG_TEE) += tee/
+obj-$(CONFIG_HAS_VPCI) += vpci.o

obj-$(CONFIG_HAS_ALTERNATIVE) += alternative.o
obj-y += bootfdt.init.o

xen/arch/arm/domain.c +4 -0
index eef0661beb..96e1b23550 100644
@@ -39,6 +39,7 @@
 #include <asm/vgic.h>
 #include <asm/vtimer.h>
 
+#include "vpci.h"
 #include "vuart.h"
 
 DEFINE_PER_CPU(struct vcpu *, curr_vcpu);
@@ -773,6 +774,9 @@ int arch_domain_create(struct domain *d,
   if ( is_hardware_domain(d) && (rc = domain_vuart_init(d)) )
       goto fail;
 
+    if ( (rc = domain_vpci_init(d)) != 0 )
+        goto fail;
+
   return 0;
 

fail:

xen/arch/arm/vpci.c new file +45 -0
new file mode 100644
index 0000000000..7c3552b65d
@@ -0,0 +1,74 @@
+/*
+ * xen/arch/arm/vpci.c
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+#include <xen/sched.h>
+#include <xen/vpci.h>
+
+#include <asm/mmio.h>
+
+static int vpci_mmio_read(struct vcpu *v, mmio_info_t *info,
+                          register_t *r, void *p)
+{
+    pci_sbdf_t sbdf;
+    /* data is needed to prevent a pointer cast on 32bit */
+    unsigned long data = ~0ul;
+    int ret;
+
+    /* We ignore segment part and always handle segment 0 */
+    sbdf.sbdf = ECAM_BDF(info->gpa);
+
+    ret = vpci_ecam_mmio_read(sbdf, ECAM_REG_OFFSET(info->gpa),
+                              1U << info->dabt.size, &data);
+
+    *r = data;
+
+    return ret;
+}
+
+static int vpci_mmio_write(struct vcpu *v, mmio_info_t *info,
+                           register_t r, void *p)
+{
+    pci_sbdf_t sbdf;
+
+    /* We ignore segment part and always handle segment 0 */
+    sbdf.sbdf = ECAM_BDF(info->gpa);
+
Quoted 8 lines

xen/arch/arm/vpci.c line 48

Quoted 3 lines
xen/arch/arm/vpci.c new file +3 -0
+    return vpci_ecam_mmio_write(sbdf, ECAM_REG_OFFSET(info->gpa),
+                                1U << info->dabt.size, r);
+}

I'm not sure returning an error value here is helpful, as I'm not sure how native Arm behaves and how this error is propagated into the guest. FWIWreturning ~0 or dropping writes is what we do in x86 when vPCI is not capable of handling the access.

Mmio handlers can take a return code on arm if something did not work so I think this is the right way to do it on arm.
Now has agreed with Jan, we will change the return type of vpci_ecam_write (also renamed) to be a boolean.

Quoted 86 lines
Quoted 86 lines
xen/arch/arm/vpci.c new file +26 -0
+
+static const struct mmio_handler_ops vpci_mmio_handler = {
+    .read  = vpci_mmio_read,
+    .write = vpci_mmio_write,
+};
+
+int domain_vpci_init(struct domain *d)
+{
+    if ( !has_vpci(d) )
+        return 0;
+
+    register_mmio_handler(d, &vpci_mmio_handler,
+                          GUEST_VPCI_ECAM_BASE, GUEST_VPCI_ECAM_SIZE, NULL);
+
+    return 0;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
+
xen/arch/arm/vpci.h new file +36 -0
new file mode 100644
index 0000000000..d8a7b0e3e8
@@ -0,0 +1,36 @@
+/*
+ * xen/arch/arm/vpci.h
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __ARCH_ARM_VPCI_H__
+#define __ARCH_ARM_VPCI_H__
+
+#ifdef CONFIG_HAS_VPCI
+int domain_vpci_init(struct domain *d);
+#else
+static inline int domain_vpci_init(struct domain *d)
+{
+    return 0;
+}
+#endif
+
+#endif /* __ARCH_ARM_VPCI_H__ */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/drivers/passthrough/pci.c +1 -1
index 3aa8c3175f..8cc529ecec 100644
@@ -658,7 +658,7 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
                  const struct pci_dev_info *info, nodeid_t node)
 {
   struct pci_seg *pseg;
-    struct pci_dev *pdev;
+    struct pci_dev *pdev = NULL;
   unsigned int slot = PCI_SLOT(devfn), func = PCI_FUNC(devfn);
   const char *pdev_type;
   int ret;
@@ -752,6 +752,19 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
 
   check_pdev(pdev);
 
Quoted 15 lines

xen/drivers/passthrough/pci.c int pci_add_device(u16 seg, u8 bus, u8 devfn, · line 766

Quoted 12 lines
xen/drivers/passthrough/pci.c +12 -0
+#ifdef CONFIG_ARM
+    /*
+     * On ARM PCI devices discovery will be done by Dom0. Add vpci handler when
+     * Dom0 inform XEN to add the PCI devices in XEN.
+     */
+    ret = vpci_add_handlers(pdev);
+    if ( ret )
+    {
+        printk(XENLOG_ERR "Setup of vPCI failed: %d\n", ret);
+        goto out;
+    }
+#endif

I think vpci_add_handlers should be called after checking that pdev->domain is != NULL, so I would move this chunk a bit below.

Quoted 4 lines

On arm this would prevent the dom0less use case or to have the PCI bus enumerated from an other domain.
@oleksandr: can you comment on this one, you might have a better answer than me on this ?

For my understanding, as also Jan pointed out, this should be put inside the If ( !pdev->domain ) because also as the comment suggests, this operation should be performed only when there is a Dom0 and when we are using a dom0less setup the pdev->domain should be NULL if I’m not wrong. @oleksandr maybe can confirm

Quoted 29 lines
Quoted 15 lines

xen/drivers/passthrough/pci.c out: · line 801

Quoted 12 lines
xen/drivers/passthrough/pci.c +3 -0
+
   ret = 0;
   if ( !pdev->domain )
   {
@@ -784,6 +797,9 @@ out:
                  &PCI_SBDF(seg, bus, slot, func));
       }
   }
+    else if ( pdev )
+        pci_cleanup_msi(pdev);

I'm slightly lost at why you add this chunk, is this strictly related to the patch?

This was discussed a lot in previous version of the patch and requested by Stefano. The idea here is that as soon as handlers are added some bits might be modified in the PCI config space leading possibly to msi interrupts. So it is safer to cleanup on the error path. For references please see discussion on v4 and v5 where this was actually added (to much references as the discussion was long so here [1] and [2] are the patchwork thread).

[1] https://patchwork.kernel.org/project/xen-devel/patch/9bdca2cda5d2e83f94dc2423e55714273539760a.1633540842.git.rahul.singh@arm.com/
[2] https://patchwork.kernel.org/project/xen-devel/patch/f093de681c2560a7196895bcd666ef8840885c1d.1633340795.git.rahul.singh@arm.com/

Regards
Bertrand

#151 lore raw

Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM.

In reply to Bertrand Marquis

Bertrand Marquis writes ("Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM."):

Quoted 12 lines
Quoted 11 lines · Jan Beulich

The latter is fine to be put here (i.e. FTAOD I'm fine with it staying here). For the former I even question its original placement in asm-x86/pci.h: It's not generally correct as per the PCI spec, as the bus portion of the address can be anywhere from 1 to 8 bits. And in fact there is a reason why this macro was/is used in only a single place, but not e.g. in x86'es handling of physical MCFG. It is merely an implementation choice in vPCI that the entire segment 0 has a linear address range covering all 256 buses. Hence I think this wants to move to xen/vpci.h and then perhaps also be named VPCI_ECAM_BDF().

Quoted 5 lines

On previous version it was request to renamed this to ECAM and agreed to put is here. Now you want me to rename it to VPCI and move it again. I would like to have a confirmation that this is ok and the final move if possible.

@Roger can you confirm this is what is wanted ?

I think Roger is not available today I'm afraid.

Bertrand, can you give me a link to the comment from Roger ? Assuming that it says what I think it will say:

I think the best thing to do will be to leave the name as it was in the most recent version of your series. I don't think it makes sense to block this patch over a naming disagreement. And it would be best to minimise unnecessary churn.

I would be happy to release-ack a name change (perhaps proposed bo Jan or Roger) supposing that that is the ultimate maintainer consensus.

Jan, would that approach be OK with you ?

Ian.

#143 lore raw

Re: [PATCH v6 1/3] xen/vpci: Move ecam access functions to common code

In reply to Roger Pau Monné
Quoted 33 lines · Roger Pau Monné
Quoted 32 lines · Bertrand Marquis
Quoted 31 lines · Roger Pau Monné
Quoted 15 lines · Bertrand Marquis
xen/arch/x86/hvm/io.c +0 -11
@@ -434,25 +420,8 @@ static int vpci_mmcfg_read(struct vcpu *v, unsigned long addr,
    reg = vpci_mmcfg_decode_addr(mmcfg, addr, &sbdf);
    read_unlock(&d->arch.hvm.mmcfg_lock);
 
-    if ( !vpci_access_allowed(reg, len) ||
-         (reg + len) > PCI_CFG_SPACE_EXP_SIZE )
-        return X86EMUL_OKAY;
-
-    /*
-     * According to the PCIe 3.1A specification:
-     *  - Configuration Reads and Writes must usually be DWORD or smaller
-     *    in size.
-     *  - Because Root Complex implementations are not required to support
-     *    accesses to a RCRB that cross DW boundaries [...] software
-     *    should take care not to cause the generation of such accesses

xen/arch/x86/hvm/io.c static int vpci_mmcfg_read(struct vcpu *v, unsigned long addr, · line 424

Quoted 10 lines
xen/arch/x86/hvm/io.c +2 -8
-     *    when accessing a RCRB unless the Root Complex will support the
-     *    access.
-     *  Xen however supports 8byte accesses by splitting them into two
-     *  4byte accesses.
-     */
-    *data = vpci_read(sbdf, reg, min(4u, len));
-    if ( len == 8 )
-        *data |= (uint64_t)vpci_read(sbdf, reg + 4, 4) << 32;
+    /* Ignore return code */
+    vpci_ecam_mmio_read(sbdf, reg, len, data);

I think it would be better for vpci_ecam_mmio_read to just return the read value, or ~0 in case of error, at least that interface would be simpler and suitable for x86.

Quoted 8 lines
Quoted 2 lines

I am not quite sure on this as on absolute to read ~0 is possible so the caller cannot distinguish between properly reading ~0 or an access allowed error.

How do you report an access allowed error on Arm for the PCI config space?

At least on x86 I don't think we currently have a way to propagate such errors, neither a plan to do so that I'm aware.

I have to admit I can't even think of how such a plan could look like, give x86'es history.

Jan

#164 lore raw

Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM.

In reply to Luca Fancellu

Hi Luca,

Quoted 296 lines · Luca Fancellu
Quoted 290 lines · Bertrand Marquis

Hi Roger,

Quoted 31 lines · Roger Pau Monné
Quoted 20 lines · Bertrand Marquis

From: Rahul Singh <[email protected]>

The existing VPCI support available for X86 is adapted for Arm. When the device is added to XEN via the hyper call “PHYSDEVOP_pci_device_add”, VPCI handler for the config space access is added to the Xen to emulate the PCI devices config space.

A MMIO trap handler for the PCI ECAM space is registered in XEN so that when guest is trying to access the PCI config space,XEN will trap the access and emulate read/write using the VPCI and not the real PCI hardware.

For Dom0less systems scan_pci_devices() would be used to discover the PCI device in XEN and VPCI handler will be added during XEN boots.

This patch is also doing some small fixes to fix compilation errors on arm32 of vpci:
- add a cast to unsigned long in print in header.c
- add a cast to uint64_t in vpci_ecam_mmio_write

Quoted 7 lines

---
Changes in v6:
- Use new vpci_ecam_ helpers for PCI access
- Do not set XEN_DOMCTL_CDF_vpci for dom0 for now (will be done in a future patch once everything is ready)

Quoted 2 lines

Isn't the series missing a revert of XEN_DOMCTL_CDF_vpci? I think that's what we agreed would be the way forward.

A separate reverse patch for that has already been merged:
https://xenbits.xen.org/gitweb/?p=xen.git;a=commit;h=9516d01ac3015f522528ed6dafb3f584eaa7ed2c

Quoted 130 lines
Quoted 130 lines

- rename REGISTER_OFFSET to ECAM_REG_OFFSET and move it to pci.h
- remove not needed local variables in vpci_mmio_write, the one in read has been kept to ensure proper compilation on arm32
- move call to vpci_add_handlers before iommu init to simplify exit path
- move call to pci_cleanup_msi in the out section of pci_add_device if pdev is not NULL and on error
- initialize pdev to NULL to handle properly exit path call of pci_cleanup_msi
- keep has_vpci to return false for now as CFG_vpci has been removed. Added a comment on top of the definition.
- fix compilation errors on arm32 (print in header.c, cast missing in mmio_write.
- local variable was kept in vpci_mmio_read on arm to prevent a cast error in arm32.
Change in v5:
- Add pci_cleanup_msi(pdev) incleanup part.
- Added Reviewed-by: Stefano Stabellini <[email protected]> Change in v4:
- Move addition of XEN_DOMCTL_CDF_vpci flag to separate patch Change in v3:
- Use is_pci_passthrough_enabled() in place of pci_passthrough_enabled variable
- Reject XEN_DOMCTL_CDF_vpci for x86 in arch_sanitise_domain_config()
- Remove IS_ENABLED(CONFIG_HAS_VPCI) from has_vpci()
Change in v2:
- Add new XEN_DOMCTL_CDF_vpci flag
- modify has_vpci() to include XEN_DOMCTL_CDF_vpci
- enable vpci support when pci-passthough option is enabled.
---
---

xen/arch/arm/Makefile         |  1 +
xen/arch/arm/domain.c         |  4 ++
xen/arch/arm/vpci.c           | 74 +++++++++++++++++++++++++++++++++++
xen/arch/arm/vpci.h           | 36 +++++++++++++++++
xen/drivers/passthrough/pci.c | 18 ++++++++-
xen/drivers/vpci/header.c     |  3 +-
xen/drivers/vpci/vpci.c       |  2 +-
xen/include/asm-arm/domain.h  |  1 +
xen/include/asm-x86/pci.h     |  2 -
xen/include/public/arch-arm.h |  7 ++++
xen/include/xen/pci.h         |  3 ++
11 files changed, 146 insertions(+), 5 deletions(-)

create mode 100644 xen/arch/arm/vpci.c
create mode 100644 xen/arch/arm/vpci.h

xen/arch/arm/Makefile +0 -0
index 64518848b2..07f634508e 100644
@@ -7,6 +7,7 @@ ifneq ($(CONFIG_NO_PLAT),y)

obj-y += platforms/
endif
obj-$(CONFIG_TEE) += tee/
+obj-$(CONFIG_HAS_VPCI) += vpci.o

obj-$(CONFIG_HAS_ALTERNATIVE) += alternative.o
obj-y += bootfdt.init.o

xen/arch/arm/domain.c +4 -0
index eef0661beb..96e1b23550 100644
@@ -39,6 +39,7 @@
 #include <asm/vgic.h>
 #include <asm/vtimer.h>
 
+#include "vpci.h"
 #include "vuart.h"
 
 DEFINE_PER_CPU(struct vcpu *, curr_vcpu);
@@ -773,6 +774,9 @@ int arch_domain_create(struct domain *d,
  if ( is_hardware_domain(d) && (rc = domain_vuart_init(d)) )
      goto fail;
 
+    if ( (rc = domain_vpci_init(d)) != 0 )
+        goto fail;
+
  return 0;
 

fail:

xen/arch/arm/vpci.c new file +45 -0
new file mode 100644
index 0000000000..7c3552b65d
@@ -0,0 +1,74 @@
+/*
+ * xen/arch/arm/vpci.c
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+#include <xen/sched.h>
+#include <xen/vpci.h>
+
+#include <asm/mmio.h>
+
+static int vpci_mmio_read(struct vcpu *v, mmio_info_t *info,
+                          register_t *r, void *p)
+{
+    pci_sbdf_t sbdf;
+    /* data is needed to prevent a pointer cast on 32bit */
+    unsigned long data = ~0ul;
+    int ret;
+
+    /* We ignore segment part and always handle segment 0 */
+    sbdf.sbdf = ECAM_BDF(info->gpa);
+
+    ret = vpci_ecam_mmio_read(sbdf, ECAM_REG_OFFSET(info->gpa),
+                              1U << info->dabt.size, &data);
+
+    *r = data;
+
+    return ret;
+}
+
+static int vpci_mmio_write(struct vcpu *v, mmio_info_t *info,
+                           register_t r, void *p)
+{
+    pci_sbdf_t sbdf;
+
+    /* We ignore segment part and always handle segment 0 */
+    sbdf.sbdf = ECAM_BDF(info->gpa);
+
Quoted 8 lines

xen/arch/arm/vpci.c line 48

Quoted 3 lines
xen/arch/arm/vpci.c new file +3 -0
+    return vpci_ecam_mmio_write(sbdf, ECAM_REG_OFFSET(info->gpa),
+                                1U << info->dabt.size, r);
+}

I'm not sure returning an error value here is helpful, as I'm not sure how native Arm behaves and how this error is propagated into the guest. FWIWreturning ~0 or dropping writes is what we do in x86 when vPCI is not capable of handling the access.

Mmio handlers can take a return code on arm if something did not work so I think this is the right way to do it on arm.
Now has agreed with Jan, we will change the return type of vpci_ecam_write (also renamed) to be a boolean.

Quoted 86 lines
Quoted 86 lines
xen/arch/arm/vpci.c new file +26 -0
+
+static const struct mmio_handler_ops vpci_mmio_handler = {
+    .read  = vpci_mmio_read,
+    .write = vpci_mmio_write,
+};
+
+int domain_vpci_init(struct domain *d)
+{
+    if ( !has_vpci(d) )
+        return 0;
+
+    register_mmio_handler(d, &vpci_mmio_handler,
+                          GUEST_VPCI_ECAM_BASE, GUEST_VPCI_ECAM_SIZE, NULL);
+
+    return 0;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
+
xen/arch/arm/vpci.h new file +36 -0
new file mode 100644
index 0000000000..d8a7b0e3e8
@@ -0,0 +1,36 @@
+/*
+ * xen/arch/arm/vpci.h
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __ARCH_ARM_VPCI_H__
+#define __ARCH_ARM_VPCI_H__
+
+#ifdef CONFIG_HAS_VPCI
+int domain_vpci_init(struct domain *d);
+#else
+static inline int domain_vpci_init(struct domain *d)
+{
+    return 0;
+}
+#endif
+
+#endif /* __ARCH_ARM_VPCI_H__ */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/drivers/passthrough/pci.c +1 -1
index 3aa8c3175f..8cc529ecec 100644
@@ -658,7 +658,7 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
                 const struct pci_dev_info *info, nodeid_t node)
 {
  struct pci_seg *pseg;
-    struct pci_dev *pdev;
+    struct pci_dev *pdev = NULL;
  unsigned int slot = PCI_SLOT(devfn), func = PCI_FUNC(devfn);
  const char *pdev_type;
  int ret;
@@ -752,6 +752,19 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
 
  check_pdev(pdev);
 
Quoted 15 lines

xen/drivers/passthrough/pci.c int pci_add_device(u16 seg, u8 bus, u8 devfn, · line 766

Quoted 12 lines
xen/drivers/passthrough/pci.c +12 -0
+#ifdef CONFIG_ARM
+    /*
+     * On ARM PCI devices discovery will be done by Dom0. Add vpci handler when
+     * Dom0 inform XEN to add the PCI devices in XEN.
+     */
+    ret = vpci_add_handlers(pdev);
+    if ( ret )
+    {
+        printk(XENLOG_ERR "Setup of vPCI failed: %d\n", ret);
+        goto out;
+    }
+#endif

I think vpci_add_handlers should be called after checking that pdev->domain is != NULL, so I would move this chunk a bit below.

Quoted 4 lines

On arm this would prevent the dom0less use case or to have the PCI bus enumerated from an other domain.
@oleksandr: can you comment on this one, you might have a better answer than me on this ?

Quoted 5 lines

For my understanding, as also Jan pointed out, this should be put inside the If ( !pdev->domain ) because also as the comment suggests, this operation should be performed only when there is a Dom0 and when we are using a dom0less setup the pdev->domain should be NULL if I’m not wrong. @oleksandr maybe can confirm

You are right.
I will fix that and move that into an if.

Regards
Bertrand

Quoted 30 lines
Quoted 30 lines
Quoted 16 lines

xen/drivers/passthrough/pci.c out: · line 801

Quoted 13 lines
xen/drivers/passthrough/pci.c +3 -0
+
  ret = 0;
  if ( !pdev->domain )
  {
@@ -784,6 +797,9 @@ out:
                 &PCI_SBDF(seg, bus, slot, func));
      }
  }
+    else if ( pdev )
+        pci_cleanup_msi(pdev);

I'm slightly lost at why you add this chunk, is this strictly related to the patch?

This was discussed a lot in previous version of the patch and requested by Stefano. The idea here is that as soon as handlers are added some bits might be modified in the PCI config space leading possibly to msi interrupts. So it is safer to cleanup on the error path. For references please see discussion on v4 and v5 where this was actually added (to much references as the discussion was long so here [1] and [2] are the patchwork thread).

[1] https://patchwork.kernel.org/project/xen-devel/patch/9bdca2cda5d2e83f94dc2423e55714273539760a.1633540842.git.rahul.singh@arm.com/
[2] https://patchwork.kernel.org/project/xen-devel/patch/f093de681c2560a7196895bcd666ef8840885c1d.1633340795.git.rahul.singh@arm.com/

Regards
Bertrand

#152 lore raw

Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM.

In reply to Ian Jackson
Quoted 29 lines · Ian Jackson

Bertrand Marquis writes ("Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM."):

Quoted 12 lines
Quoted 11 lines · Jan Beulich

The latter is fine to be put here (i.e. FTAOD I'm fine with it staying here). For the former I even question its original placement in asm-x86/pci.h: It's not generally correct as per the PCI spec, as the bus portion of the address can be anywhere from 1 to 8 bits. And in fact there is a reason why this macro was/is used in only a single place, but not e.g. in x86'es handling of physical MCFG. It is merely an implementation choice in vPCI that the entire segment 0 has a linear address range covering all 256 buses. Hence I think this wants to move to xen/vpci.h and then perhaps also be named VPCI_ECAM_BDF().

Quoted 5 lines

On previous version it was request to renamed this to ECAM and agreed to put is here. Now you want me to rename it to VPCI and move it again. I would like to have a confirmation that this is ok and the final move if possible.

@Roger can you confirm this is what is wanted ?

I think Roger is not available today I'm afraid.

Bertrand, can you give me a link to the comment from Roger ? Assuming that it says what I think it will say:

I think the best thing to do will be to leave the name as it was in the most recent version of your series. I don't think it makes sense to block this patch over a naming disagreement. And it would be best to minimise unnecessary churn.

Quoted 4 lines

I would be happy to release-ack a name change (perhaps proposed bo Jan or Roger) supposing that that is the ultimate maintainer consensus.

Jan, would that approach be OK with you ?

Well, yes, if a subsequent name change is okay, then I could live with that. I'd still find it odd to rename a function immediately after it already got renamed. As expressed elsewhere, I suspect in his request Roger did not pay attention to a use of the function in non-ECAM code.

Jan

#165 lore raw

Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM.

In reply to Bertrand Marquis
Quoted 39 lines · Bertrand Marquis

Hi Roger,

Quoted 21 lines · Roger Pau Monné
Quoted 18 lines · Bertrand Marquis

From: Rahul Singh <[email protected]>

xen/drivers/passthrough/pci.c +1 -1
index 3aa8c3175f..8cc529ecec 100644
@@ -658,7 +658,7 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
                   const struct pci_dev_info *info, nodeid_t node)
 {
    struct pci_seg *pseg;
-    struct pci_dev *pdev;
+    struct pci_dev *pdev = NULL;
    unsigned int slot = PCI_SLOT(devfn), func = PCI_FUNC(devfn);
    const char *pdev_type;
    int ret;
@@ -752,6 +752,19 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
 
    check_pdev(pdev);
 
Quoted 15 lines

xen/drivers/passthrough/pci.c int pci_add_device(u16 seg, u8 bus, u8 devfn, · line 766

Quoted 12 lines
xen/drivers/passthrough/pci.c +12 -0
+#ifdef CONFIG_ARM
+    /*
+     * On ARM PCI devices discovery will be done by Dom0. Add vpci handler when
+     * Dom0 inform XEN to add the PCI devices in XEN.
+     */
+    ret = vpci_add_handlers(pdev);
+    if ( ret )
+    {
+        printk(XENLOG_ERR "Setup of vPCI failed: %d\n", ret);
+        goto out;
+    }
+#endif

I think vpci_add_handlers should be called after checking that pdev->domain is != NULL, so I would move this chunk a bit below.

Quoted 2 lines

On arm this would prevent the dom0less use case or to have the PCI bus enumerated from an other domain.

vpci_add_handlers will try to access pdev->domain, so passing a pdev without domain being set is wrong.

Quoted 18 lines

@oleksandr: can you comment on this one, you might have a better answer than me on this ?

Quoted 14 lines

xen/drivers/passthrough/pci.c out: · line 801

Quoted 11 lines
xen/drivers/passthrough/pci.c +3 -0
+
    ret = 0;
    if ( !pdev->domain )
    {
@@ -784,6 +797,9 @@ out:
                   &PCI_SBDF(seg, bus, slot, func));
        }
    }
+    else if ( pdev )
+        pci_cleanup_msi(pdev);

I'm slightly lost at why you add this chunk, is this strictly related to the patch?

Quoted 7 lines

This was discussed a lot in previous version of the patch and requested by Stefano. The idea here is that as soon as handlers are added some bits might be modified in the PCI config space leading possibly to msi interrupts. So it is safer to cleanup on the error path. For references please see discussion on v4 and v5 where this was actually added (to much references as the discussion was long so here [1] and [2] are the patchwork thread).

pci_add_device being solely used by trusted domains, I think it would be fine to require that the domain doesn't poke the PCI config space until the call to pci_add_device has finished. Else it's likely to get inconsistent results, or mess up with the device state.

In any case, such addition needs some kind of reasoning added to the commit message if it's really required.

Thanks, Roger.

#167 lore raw

Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM.

In reply to Bertrand Marquis
Quoted 22 lines · Bertrand Marquis
Quoted 6 lines · Roger Pau Monné
Quoted 4 lines · Bertrand Marquis
xen/drivers/passthrough/pci.c +0 -0
@@ -752,6 +752,19 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
 
    check_pdev(pdev);
 
Quoted 15 lines

xen/drivers/passthrough/pci.c int pci_add_device(u16 seg, u8 bus, u8 devfn, · line 766

Quoted 12 lines
xen/drivers/passthrough/pci.c +12 -0
+#ifdef CONFIG_ARM
+    /*
+     * On ARM PCI devices discovery will be done by Dom0. Add vpci handler when
+     * Dom0 inform XEN to add the PCI devices in XEN.
+     */
+    ret = vpci_add_handlers(pdev);
+    if ( ret )
+    {
+        printk(XENLOG_ERR "Setup of vPCI failed: %d\n", ret);
+        goto out;
+    }
+#endif

I think vpci_add_handlers should be called after checking that pdev->domain is != NULL, so I would move this chunk a bit below.

Quoted 4 lines

On arm this would prevent the dom0less use case or to have the PCI bus enumerated from an other domain.
@oleksandr: can you comment on this one, you might have a better answer than me on this ?

Well, without Xen doing the enumeration, some other entity would need to do so, including the reporting to Xen. Obviously without a Dom0 it would be ambiguous which domain to assign the device to; perhaps it should be the caller in this case? That would make that caller domain a pseudo-hwdom though, as far as PCI is concerned, which may not be desirable according to my (limited) understanding of dom0less.

Quoted 18 lines
Quoted 9 lines

xen/drivers/passthrough/pci.c out: · line 801

Quoted 6 lines
xen/drivers/passthrough/pci.c +2 -0
@@ -784,6 +797,9 @@ out:
                   &PCI_SBDF(seg, bus, slot, func));
        }
    }
+    else if ( pdev )
+        pci_cleanup_msi(pdev);

I'm slightly lost at why you add this chunk, is this strictly related to the patch?

This was discussed a lot in previous version of the patch and requested by Stefano. The idea here is that as soon as handlers are added some bits might be modified in the PCI config space leading possibly to msi interrupts. So it is safer to cleanup on the error path. For references please see discussion on v4 and v5 where this was actually added (to much references as the discussion was long so here [1] and [2] are the patchwork thread).

Quoted 2 lines

[1] https://patchwork.kernel.org/project/xen-devel/patch/9bdca2cda5d2e83f94dc2423e55714273539760a.1633540842.git.rahul.singh@arm.com/
[2] https://patchwork.kernel.org/project/xen-devel/patch/f093de681c2560a7196895bcd666ef8840885c1d.1633340795.git.rahul.singh@arm.com/

The addition of this call has repeatedly raised questions. This is a good indication that sufficient discussion thereof has been lacking from the patch description.

Jan

#166 lore raw

Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM.

In reply to Roger Pau Monné

Hi,

Quoted 42 lines · Roger Pau Monné
Quoted 39 lines · Bertrand Marquis

Hi Roger,

Quoted 36 lines · Roger Pau Monné
Quoted 20 lines · Bertrand Marquis

From: Rahul Singh <[email protected]>

xen/drivers/passthrough/pci.c +3 -1
index 3aa8c3175f..8cc529ecec 100644
@@ -658,7 +658,7 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
                  const struct pci_dev_info *info, nodeid_t node)
 {
   struct pci_seg *pseg;
-    struct pci_dev *pdev;
+    struct pci_dev *pdev = NULL;
   unsigned int slot = PCI_SLOT(devfn), func = PCI_FUNC(devfn);
   const char *pdev_type;
   int ret;
@@ -752,6 +752,19 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
 
   check_pdev(pdev);
 
+#ifdef CONFIG_ARM
+    /*

xen/drivers/passthrough/pci.c int pci_add_device(u16 seg, u8 bus, u8 devfn, · line 766

Quoted 10 lines
xen/drivers/passthrough/pci.c +10 -0
+     * On ARM PCI devices discovery will be done by Dom0. Add vpci handler when
+     * Dom0 inform XEN to add the PCI devices in XEN.
+     */
+    ret = vpci_add_handlers(pdev);
+    if ( ret )
+    {
+        printk(XENLOG_ERR "Setup of vPCI failed: %d\n", ret);
+        goto out;
+    }
+#endif

I think vpci_add_handlers should be called after checking that pdev->domain is != NULL, so I would move this chunk a bit below.

Quoted 5 lines
Quoted 2 lines

On arm this would prevent the dom0less use case or to have the PCI bus enumerated from an other domain.

vpci_add_handlers will try to access pdev->domain, so passing a pdev without domain being set is wrong.

Right and the initial comment from Rahul in the code is saying so. I will move the block inside the if.

Quoted 27 lines
Quoted 19 lines

@oleksandr: can you comment on this one, you might have a better answer than me on this ?

Quoted 14 lines

xen/drivers/passthrough/pci.c out: · line 801

Quoted 11 lines
xen/drivers/passthrough/pci.c +3 -0
+
   ret = 0;
   if ( !pdev->domain )
   {
@@ -784,6 +797,9 @@ out:
                  &PCI_SBDF(seg, bus, slot, func));
       }
   }
+    else if ( pdev )
+        pci_cleanup_msi(pdev);

I'm slightly lost at why you add this chunk, is this strictly related to the patch?

Quoted 7 lines

This was discussed a lot in previous version of the patch and requested by Stefano. The idea here is that as soon as handlers are added some bits might be modified in the PCI config space leading possibly to msi interrupts. So it is safer to cleanup on the error path. For references please see discussion on v4 and v5 where this was actually added (to much references as the discussion was long so here [1] and [2] are the patchwork thread).

Quoted 4 lines

pci_add_device being solely used by trusted domains, I think it would be fine to require that the domain doesn't poke the PCI config space until the call to pci_add_device has finished. Else it's likely to get inconsistent results, or mess up with the device state.

Ack.

Quoted 3 lines

In any case, such addition needs some kind of reasoning added to the commit message if it's really required.

With the code moving inside the following else and pci_cleanup_msi being empty for now on arm, I will remove the pci_cleanup_msi as it does not related directly to this change for now and might change the behaviour on x86.

If this is needed at some point due to arm, this will be done once msi support will be added.

Regards
Bertrand

Quoted 2 lines

Thanks, Roger.

#168 lore raw

Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM.

In reply to Jan Beulich
Quoted 32 lines · Jan Beulich
Quoted 22 lines · Bertrand Marquis
Quoted 6 lines · Roger Pau Monné
Quoted 4 lines · Bertrand Marquis
xen/drivers/passthrough/pci.c +0 -0
@@ -752,6 +752,19 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
 
   check_pdev(pdev);
 
Quoted 15 lines

xen/drivers/passthrough/pci.c int pci_add_device(u16 seg, u8 bus, u8 devfn, · line 766

Quoted 12 lines
xen/drivers/passthrough/pci.c +12 -0
+#ifdef CONFIG_ARM
+    /*
+     * On ARM PCI devices discovery will be done by Dom0. Add vpci handler when
+     * Dom0 inform XEN to add the PCI devices in XEN.
+     */
+    ret = vpci_add_handlers(pdev);
+    if ( ret )
+    {
+        printk(XENLOG_ERR "Setup of vPCI failed: %d\n", ret);
+        goto out;
+    }
+#endif

I think vpci_add_handlers should be called after checking that pdev->domain is != NULL, so I would move this chunk a bit below.

Quoted 4 lines

On arm this would prevent the dom0less use case or to have the PCI bus enumerated from an other domain.
@oleksandr: can you comment on this one, you might have a better answer than me on this ?

Quoted 6 lines

Well, without Xen doing the enumeration, some other entity would need to do so, including the reporting to Xen. Obviously without a Dom0 it would be ambiguous which domain to assign the device to; perhaps it should be the caller in this case? That would make that caller domain a pseudo-hwdom though, as far as PCI is concerned, which may not be desirable according to my (limited) understanding of dom0less.

This is not really related to this patch but the plan is the following:
- enumeration would have to be done by the firmware or boot loader before
- xen will have some code to detect PCI devices
- dom0less can be used to assign PCI devices to guest

Anyway does not change the fact that this must be called when domain is not NULL and I will fix that.

Quoted 19 lines
Quoted 19 lines
Quoted 10 lines

xen/drivers/passthrough/pci.c out: · line 801

Quoted 7 lines
xen/drivers/passthrough/pci.c +2 -0
@@ -784,6 +797,9 @@ out:
                  &PCI_SBDF(seg, bus, slot, func));
       }
   }
+    else if ( pdev )
+        pci_cleanup_msi(pdev);

I'm slightly lost at why you add this chunk, is this strictly related to the patch?

This was discussed a lot in previous version of the patch and requested by Stefano. The idea here is that as soon as handlers are added some bits might be modified in the PCI config space leading possibly to msi interrupts. So it is safer to cleanup on the error path. For references please see discussion on v4 and v5 where this was actually added (to much references as the discussion was long so here [1] and [2] are the patchwork thread).

Quoted 6 lines
Quoted 2 lines

[1] https://patchwork.kernel.org/project/xen-devel/patch/9bdca2cda5d2e83f94dc2423e55714273539760a.1633540842.git.rahul.singh@arm.com/
[2] https://patchwork.kernel.org/project/xen-devel/patch/f093de681c2560a7196895bcd666ef8840885c1d.1633340795.git.rahul.singh@arm.com/

The addition of this call has repeatedly raised questions. This is a good indication that sufficient discussion thereof has been lacking from the patch description.

Yes and I will remove it as it only impacts x86 right now.
If this is needed, we will have to do it while adding MSI support on Arm.

Regards
Bertrand

Quoted 3 lines

Jan

#159 lore raw

Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM.

In reply to Bertrand Marquis
Quoted 33 lines · Bertrand Marquis
Quoted 24 lines · Jan Beulich
Quoted 9 lines · Bertrand Marquis
xen/drivers/passthrough/pci.c +5 -0
@@ -752,6 +752,19 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
 
    check_pdev(pdev);
 
+#ifdef CONFIG_ARM
+    /*
+     * On ARM PCI devices discovery will be done by Dom0. Add vpci handler when
+     * Dom0 inform XEN to add the PCI devices in XEN.
+     */

xen/drivers/passthrough/pci.c int pci_add_device(u16 seg, u8 bus, u8 devfn, · line 769

Quoted 10 lines
xen/drivers/passthrough/pci.c +8 -0
+    ret = vpci_add_handlers(pdev);
+    if ( ret )
+    {
+        printk(XENLOG_ERR "Setup of vPCI failed: %d\n", ret);
+        goto out;
+    }
+#endif
+
    ret = 0;
    if ( !pdev->domain )

Elsewhere I did point out that you need to be careful wrt pdev->domain being NULL. As the code in context clearly documents, you're now

Quoted 8 lines

adding handlers before that field was set. In comments to a prior version I did already suggest to consider placing the new code inside the if() in question (albeit at the time this was mainly to make clear that error cleanup may not fiddle with things not put in place by the iommu_enable_device() alternative path). This would have the further benefit of making is crystal clear that here only handlers for Dom0 would get put in place (and hence their installing for DomU-s would need to be looked for elsewhere).

Quoted 3 lines

I asked Oleksandr for confirmation here but on arm there will be 2 other use cases:
- PCI own by a DomD so device enumeration done from there
- dom0less with devices detection done inside Xen

Question is whether at this time you mean to handle all these cases. Installing handlers when device detection happens in Xen might need to be done differently, for example - e.g. more along the lines of what x86 PVH Dom0 has done.

Anything you don't mean to handle (and is safe to be left out, i.e. without breaking existing cases) will want spelling out in the description.

Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
xen/drivers/passthrough/pci.c +1 -0
@@ -784,6 +797,9 @@ out:
                   &PCI_SBDF(seg, bus, slot, func));
        }
    }
+    else if ( pdev )
Quoted 8 lines

xen/drivers/passthrough/pci.c out: · line 801

Quoted 1 line
xen/drivers/passthrough/pci.c +1 -0
+        pci_cleanup_msi(pdev);

Have you thoroughly checked that this call is benign on x86? There's no wording to that effect in the description afaics, and I can't easily convince myself that it would be correct when the iommu_enable_device() path was taken. (I'm also not going to exclude that it should have been there even prior to your work, albeit then adding this would want to be a separate bugfix patch.)

Quoted 3 lines

This was not in the original serie and requested by Stefano. I must admit I am not completely sure on the details here so I am really ok to remove this but this would go against what was requested on the previous versions (4 and 5).

I understand this, but a request to add something still requires that it be checked that the addition can't have bad effects in other cases. A compromise here (which I wouldn't like very much) might be to add another #ifdef CONFIG_ARM, solely to leave the existing x86 case undisturbed. Yet then, with the called function doing nothing on Arm, not adding the code here might as well be an option (which it looked like Stefano would be amenable to). A TODO item would then perhaps best be left here in a comment.

Quoted 20 lines
Quoted 6 lines
Quoted 6 lines
xen/include/xen/pci.h +0 -0
@@ -40,6 +40,9 @@
 #define PCI_SBDF3(s,b,df) \
    ((pci_sbdf_t){ .sbdf = (((s) & 0xffff) << 16) | PCI_BDF2(b, df) })
 
Quoted 13 lines

xen/include/xen/pci.h line 44

Quoted 2 lines
xen/include/xen/pci.h +2 -0
+#define ECAM_BDF(addr)         ( ((addr) & 0x0ffff000) >> 12)
+#define ECAM_REG_OFFSET(addr)  ((addr) & 0x00000fff)

The latter is fine to be put here (i.e. FTAOD I'm fine with it staying here). For the former I even question its original placement in asm-x86/pci.h: It's not generally correct as per the PCI spec, as the bus portion of the address can be anywhere from 1 to 8 bits. And in fact there is a reason why this macro was/is used in only a single place, but not e.g. in x86'es handling of physical MCFG. It is merely an implementation choice in vPCI that the entire segment 0 has a linear address range covering all 256 buses. Hence I think this wants to move to xen/vpci.h and then perhaps also be named VPCI_ECAM_BDF().

Quoted 4 lines

On previous version it was request to renamed this to ECAM and agreed to put is here. Now you want me to rename it to VPCI and move it again. I would like to have a confirmation that this is ok and the final move if possible.

A final confirmation is, unfortunately, only as final as it can be at the very moment it is given. It was the MCFG vs ECAM naming discussion which made me pull out again the section in the spec, reminding me of aspects I didn't previously take into consideration. I'm sorry for this, but it's an iterative process on all sides.

So, FTAOD, as a maintainer of this headed I will continue to object to a non-spec-compliant construct to be put into here. It'll be Roger, being the vPCI maintainer, to confirm that putting it in xen/vpci.h is fine.

Quoted 1 line

Also if I have to do this I will do the same for REG_OFFSET of course.

As said - that one, being in line with the PCI spec, is fine to remain as and where you have it.

Jan

#169 lore raw

Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM.

In reply to Bertrand Marquis
Quoted 36 lines · Bertrand Marquis
Quoted 29 lines · Jan Beulich
Quoted 22 lines · Bertrand Marquis
Quoted 6 lines · Roger Pau Monné
Quoted 4 lines · Bertrand Marquis
xen/drivers/passthrough/pci.c +0 -0
@@ -752,6 +752,19 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
 
   check_pdev(pdev);
 
Quoted 15 lines

xen/drivers/passthrough/pci.c int pci_add_device(u16 seg, u8 bus, u8 devfn, · line 766

Quoted 12 lines
xen/drivers/passthrough/pci.c +12 -0
+#ifdef CONFIG_ARM
+    /*
+     * On ARM PCI devices discovery will be done by Dom0. Add vpci handler when
+     * Dom0 inform XEN to add the PCI devices in XEN.
+     */
+    ret = vpci_add_handlers(pdev);
+    if ( ret )
+    {
+        printk(XENLOG_ERR "Setup of vPCI failed: %d\n", ret);
+        goto out;
+    }
+#endif

I think vpci_add_handlers should be called after checking that pdev->domain is != NULL, so I would move this chunk a bit below.

Quoted 4 lines

On arm this would prevent the dom0less use case or to have the PCI bus enumerated from an other domain.
@oleksandr: can you comment on this one, you might have a better answer than me on this ?

Quoted 6 lines

Well, without Xen doing the enumeration, some other entity would need to do so, including the reporting to Xen. Obviously without a Dom0 it would be ambiguous which domain to assign the device to; perhaps it should be the caller in this case? That would make that caller domain a pseudo-hwdom though, as far as PCI is concerned, which may not be desirable according to my (limited) understanding of dom0less.

Quoted 7 lines

This is not really related to this patch but the plan is the following:
- enumeration would have to be done by the firmware or boot loader before
- xen will have some code to detect PCI devices
- dom0less can be used to assign PCI devices to guest

Anyway does not change the fact that this must be called when domain is not NULL and I will fix that.

Since we now all seem to agree that the NULL would have been a problem, may I ask in how far any of this has actually been tested?

Jan

#170 lore raw

Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM.

Hi Jan,

Quoted 44 lines · Jan Beulich
Quoted 41 lines · Bertrand Marquis
Quoted 29 lines · Jan Beulich
Quoted 22 lines · Bertrand Marquis
Quoted 6 lines · Roger Pau Monné
Quoted 4 lines · Bertrand Marquis
xen/drivers/passthrough/pci.c +0 -0
@@ -752,6 +752,19 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
 
  check_pdev(pdev);
 
Quoted 15 lines

xen/drivers/passthrough/pci.c int pci_add_device(u16 seg, u8 bus, u8 devfn, · line 766

Quoted 12 lines
xen/drivers/passthrough/pci.c +12 -0
+#ifdef CONFIG_ARM
+    /*
+     * On ARM PCI devices discovery will be done by Dom0. Add vpci handler when
+     * Dom0 inform XEN to add the PCI devices in XEN.
+     */
+    ret = vpci_add_handlers(pdev);
+    if ( ret )
+    {
+        printk(XENLOG_ERR "Setup of vPCI failed: %d\n", ret);
+        goto out;
+    }
+#endif

I think vpci_add_handlers should be called after checking that pdev->domain is != NULL, so I would move this chunk a bit below.

Quoted 4 lines

On arm this would prevent the dom0less use case or to have the PCI bus enumerated from an other domain.
@oleksandr: can you comment on this one, you might have a better answer than me on this ?

Quoted 6 lines

Well, without Xen doing the enumeration, some other entity would need to do so, including the reporting to Xen. Obviously without a Dom0 it would be ambiguous which domain to assign the device to; perhaps it should be the caller in this case? That would make that caller domain a pseudo-hwdom though, as far as PCI is concerned, which may not be desirable according to my (limited) understanding of dom0less.

This is not really related to this patch but the plan is the following:
- enumeration would have to be done by the firmware or boot loader before
- xen will have some code to detect PCI devices
- dom0less can be used to assign PCI devices to guest

Quoted 5 lines
Quoted 2 lines

Anyway does not change the fact that this must be called when domain is not NULL and I will fix that.

Since we now all seem to agree that the NULL would have been a problem, may I ask in how far any of this has actually been tested?

With the whole serie currently on gitlab we have extensively tested passing through PCI devices on Arm in several configuration (number of device, MSI, MSI-X) and check that PCI was still functional on x86.

With the patches pushed to Xen right now it was checked that:
- xen compiles properly on arm32, arm64 and x86
- xen compiles properly with VPCI activated (using a patch) on arm32 and arm64
- xen on x86 is functionnal (using basic test on QEMU)
- xen on arm64 is functionnal (with some extensive tests on different targets)

We are only lacking some actual testing on arm32 internally.

Regards
Bertrand

Quoted 2 lines

Jan

#171 lore raw

Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM.

Quoted 56 lines · Bertrand Marquis

Hi Jan,

Quoted 44 lines · Jan Beulich
Quoted 41 lines · Bertrand Marquis
Quoted 29 lines · Jan Beulich
Quoted 22 lines · Bertrand Marquis
Quoted 6 lines · Roger Pau Monné
Quoted 4 lines · Bertrand Marquis
(quoted diff) +0 -0
@@ -752,6 +752,19 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
 
  check_pdev(pdev);
 
Quoted 15 lines

Review int pci_add_device(u16 seg, u8 bus, u8 devfn, · line 766

Quoted 12 lines
(quoted diff) +12 -0
+#ifdef CONFIG_ARM
+    /*
+     * On ARM PCI devices discovery will be done by Dom0. Add vpci handler when
+     * Dom0 inform XEN to add the PCI devices in XEN.
+     */
+    ret = vpci_add_handlers(pdev);
+    if ( ret )
+    {
+        printk(XENLOG_ERR "Setup of vPCI failed: %d\n", ret);
+        goto out;
+    }
+#endif

I think vpci_add_handlers should be called after checking that pdev->domain is != NULL, so I would move this chunk a bit below.

Quoted 4 lines

On arm this would prevent the dom0less use case or to have the PCI bus enumerated from an other domain.
@oleksandr: can you comment on this one, you might have a better answer than me on this ?

Quoted 6 lines

Well, without Xen doing the enumeration, some other entity would need to do so, including the reporting to Xen. Obviously without a Dom0 it would be ambiguous which domain to assign the device to; perhaps it should be the caller in this case? That would make that caller domain a pseudo-hwdom though, as far as PCI is concerned, which may not be desirable according to my (limited) understanding of dom0less.

This is not really related to this patch but the plan is the following:
- enumeration would have to be done by the firmware or boot loader before
- xen will have some code to detect PCI devices
- dom0less can be used to assign PCI devices to guest

Quoted 5 lines
Quoted 2 lines

Anyway does not change the fact that this must be called when domain is not NULL and I will fix that.

Since we now all seem to agree that the NULL would have been a problem, may I ask in how far any of this has actually been tested?

With the whole serie currently on gitlab we have extensively tested passing through PCI devices on Arm in several configuration (number of device, MSI, MSI-X) and check that PCI was still functional on x86.

Quoted 5 lines

With the patches pushed to Xen right now it was checked that:
- xen compiles properly on arm32, arm64 and x86
- xen compiles properly with VPCI activated (using a patch) on arm32 and arm64
- xen on x86 is functionnal (using basic test on QEMU)
- xen on arm64 is functionnal (with some extensive tests on different targets)

But somehow in the testing done you must have avoided the code path in question, or else you would have observed a crash.

Jan

#183 lore raw

Re: [PATCH v6 3/3] arm/libxl: Emulated PCI device tree node in libxl [and 1 more messages]

In reply to Ian Jackson

Hi,

Quoted 18 lines · Ian Jackson

Julien Grall writes ("Re: [PATCH v6 3/3] arm/libxl: Emulated PCI device tree node in libxl [and 1 more messages]"):

Quoted 1 line · Ian Jackson

...

Quoted 8 lines
Quoted 3 lines

That is, as I understand it on ARM vpci should be enabled if passthrough is enabled and not otherwise. That is precisely what the variable c_info->passthrough is.

On Arm, c_info->passthrough is also set when assigning platform devives (e.g. a non-PCI network card). At least for now, we don't want to create a node for vCPI in the Device-Tree because we don't enable the emulation. So...

Oh.

Quoted 4 lines
Quoted 3 lines

3. Now you can use d_config->c_info.passthrough to gate the addition of the additional stuff to the DT. Ie, that is a respin of this patch 3/3.

Quoted 8 lines
Quoted 1 line

... we will need to check d_config->num_pcidevs for the time being.

OK.

Can you leave a comment somewhere (near where c_info.passthrough is set) pointing to this use of num_pcidevs ? That might save someone some future confusion.

Here, c_info->passthrough is set to enabled if either d_config->num_pcidevs or d_config->num_dtdevs is set. Do you think we need to add there additional comment? If so can you please help with what should I write there?

BTW. None of the patch I'm preparing with regards to this discussion modifies libxl_create where c_info.passthrough i set. Do you still want me to add some comment there?

Quoted 3 lines

Thanks,
Ian.

Cheers,
Michal

#184 lore raw

Re: [PATCH v6 3/3] arm/libxl: Emulated PCI device tree node in libxl [and 1 more messages]

Quoted 25 lines · Michal Orzel

Hi,

Quoted 22 lines · Ian Jackson

Julien Grall writes ("Re: [PATCH v6 3/3] arm/libxl: Emulated PCI device tree node in libxl [and 1 more messages]"):

Quoted 1 line · Ian Jackson

...

Quoted 8 lines
Quoted 3 lines

That is, as I understand it on ARM vpci should be enabled if passthrough is enabled and not otherwise. That is precisely what the variable c_info->passthrough is.

On Arm, c_info->passthrough is also set when assigning platform devives (e.g. a non-PCI network card). At least for now, we don't want to create a node for vCPI in the Device-Tree because we don't enable the emulation. So...

Oh.

Quoted 5 lines
Quoted 3 lines

3. Now you can use d_config->c_info.passthrough to gate the addition of the additional stuff to the DT. Ie, that is a respin of this patch 3/3.

... we will need to check d_config->num_pcidevs for the time being.

OK.

Quoted 8 lines
Quoted 4 lines

Can you leave a comment somewhere (near where c_info.passthrough is set) pointing to this use of num_pcidevs ? That might save someone some future confusion.

Here, c_info->passthrough is set to enabled if either d_config->num_pcidevs or d_config->num_dtdevs is set. Do you think we need to add there additional comment? If so can you please help with what should I write there?

I forgot to add a link:
https://xenbits.xen.org/gitweb/?p=xen.git;a=blob;f=tools/libs/light/libxl_create.c;h=6ebb2bfc768d060fb898619be907fc973375cce5;hb=HEAD#l1099

Quoted 8 lines

BTW. None of the patch I'm preparing with regards to this discussion modifies libxl_create where c_info.passthrough i set. Do you still want me to add some comment there?

Quoted 3 lines

Thanks,
Ian.

Cheers,
Michal

#172 lore raw

Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM.

In reply to Jan Beulich

Hi,

Quoted 59 lines · Jan Beulich
Quoted 56 lines · Bertrand Marquis

Hi Jan,

Quoted 44 lines · Jan Beulich
Quoted 41 lines · Bertrand Marquis
Quoted 29 lines · Jan Beulich
Quoted 22 lines · Bertrand Marquis
Quoted 6 lines · Roger Pau Monné
Quoted 4 lines · Bertrand Marquis
(quoted diff) +0 -0
@@ -752,6 +752,19 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
 
 check_pdev(pdev);
 
Quoted 15 lines

Review int pci_add_device(u16 seg, u8 bus, u8 devfn, · line 766

Quoted 12 lines
(quoted diff) +12 -0
+#ifdef CONFIG_ARM
+    /*
+     * On ARM PCI devices discovery will be done by Dom0. Add vpci handler when
+     * Dom0 inform XEN to add the PCI devices in XEN.
+     */
+    ret = vpci_add_handlers(pdev);
+    if ( ret )
+    {
+        printk(XENLOG_ERR "Setup of vPCI failed: %d\n", ret);
+        goto out;
+    }
+#endif

I think vpci_add_handlers should be called after checking that pdev->domain is != NULL, so I would move this chunk a bit below.

Quoted 4 lines

On arm this would prevent the dom0less use case or to have the PCI bus enumerated from an other domain.
@oleksandr: can you comment on this one, you might have a better answer than me on this ?

Quoted 6 lines

Well, without Xen doing the enumeration, some other entity would need to do so, including the reporting to Xen. Obviously without a Dom0 it would be ambiguous which domain to assign the device to; perhaps it should be the caller in this case? That would make that caller domain a pseudo-hwdom though, as far as PCI is concerned, which may not be desirable according to my (limited) understanding of dom0less.

This is not really related to this patch but the plan is the following:
- enumeration would have to be done by the firmware or boot loader before
- xen will have some code to detect PCI devices
- dom0less can be used to assign PCI devices to guest

Quoted 5 lines
Quoted 2 lines

Anyway does not change the fact that this must be called when domain is not NULL and I will fix that.

Since we now all seem to agree that the NULL would have been a problem, may I ask in how far any of this has actually been tested?

With the whole serie currently on gitlab we have extensively tested passing through PCI devices on Arm in several configuration (number of device, MSI, MSI-X) and check that PCI was still functional on x86.

Quoted 8 lines
Quoted 5 lines

With the patches pushed to Xen right now it was checked that:
- xen compiles properly on arm32, arm64 and x86
- xen compiles properly with VPCI activated (using a patch) on arm32 and arm64
- xen on x86 is functionnal (using basic test on QEMU)
- xen on arm64 is functionnal (with some extensive tests on different targets)

But somehow in the testing done you must have avoided the code path in question, or else you would have observed a crash.

Device_add is always called by dom0 so we did not have issues here. Dom0less PCI passthrough implementation is not done right now.

So yes we did not go through this path.

Bertrand

Quoted 3 lines

Jan

#153 lore raw

Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM.

In reply to Jan Beulich
Quoted 33 lines · Jan Beulich
Quoted 32 lines · Ian Jackson

Bertrand Marquis writes ("Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM."):

Quoted 12 lines
Quoted 11 lines · Jan Beulich

The latter is fine to be put here (i.e. FTAOD I'm fine with it staying here). For the former I even question its original placement in asm-x86/pci.h: It's not generally correct as per the PCI spec, as the bus portion of the address can be anywhere from 1 to 8 bits. And in fact there is a reason why this macro was/is used in only a single place, but not e.g. in x86'es handling of physical MCFG. It is merely an implementation choice in vPCI that the entire segment 0 has a linear address range covering all 256 buses. Hence I think this wants to move to xen/vpci.h and then perhaps also be named VPCI_ECAM_BDF().

Quoted 5 lines

On previous version it was request to renamed this to ECAM and agreed to put is here. Now you want me to rename it to VPCI and move it again. I would like to have a confirmation that this is ok and the final move if possible.

@Roger can you confirm this is what is wanted ?

I think Roger is not available today I'm afraid.

Bertrand, can you give me a link to the comment from Roger ? Assuming that it says what I think it will say:

I think the best thing to do will be to leave the name as it was in the most recent version of your series. I don't think it makes sense to block this patch over a naming disagreement. And it would be best to minimise unnecessary churn.

I would be happy to release-ack a name change (perhaps proposed bo Jan or Roger) supposing that that is the ultimate maintainer consensus.

Quoted 6 lines
Quoted 1 line

Jan, would that approach be OK with you ?

Well, yes, if a subsequent name change is okay, then I could live with that. I'd still find it odd to rename a function immediately after it already got renamed. As expressed elsewhere, I suspect in his request Roger did not pay attention to a use of the function in non-ECAM code.

Using MMCFG_BDF was original requested by Julien, not myself I think:

https://lore.kernel.org/xen-devel/[email protected]/

I'm slightly loss in so many messages. On x86 we subtract the MCFG start address from the passed one before getting the BDF, and then we add the startting bus address passed in the ACPI table. This is so far not need on Arm AFAICT because of the fixed nature of the selected virtual ECAM region.

Thanks, Roger.

#185 lore raw

Re: [PATCH v6 3/3] arm/libxl: Emulated PCI device tree node in libxl [and 1 more messages]

In reply to Michal Orzel

Michal Orzel writes ("Re: [PATCH v6 3/3] arm/libxl: Emulated PCI device tree node in libxl [and 1 more messages]"):

Quoted 11 lines · Ian Jackson
Quoted 3 lines

Can you leave a comment somewhere (near where c_info.passthrough is set) pointing to this use of num_pcidevs ? That might save someone some future confusion.

Here, c_info->passthrough is set to enabled if either d_config->num_pcidevs or d_config->num_dtdevs is set. Do you think we need to add there additional comment? If so can you please help with what should I write there?

BTW. None of the patch I'm preparing with regards to this discussion modifies libxl_create where c_info.passthrough i set. Do you still want me to add some comment there?

Yes, I think so. I won't insisting on it if you feel it doesn't make sense.

Maybe something like

// NB, on ARM, libxl__arch_blah directly examines num_pcidevs to // decide whether to enable vpci, rather than using c_info->passthrough. // This will be insufficient if and when ARM does PCI hotplug.

?

Ian.

#186 lore raw

Re: [PATCH v6 3/3] arm/libxl: Emulated PCI device tree node in libxl [and 1 more messages]

Quoted 13 lines · Ian Jackson

Michal Orzel writes ("Re: [PATCH v6 3/3] arm/libxl: Emulated PCI device tree node in libxl [and 1 more messages]"):

Quoted 11 lines · Ian Jackson
Quoted 3 lines

Can you leave a comment somewhere (near where c_info.passthrough is set) pointing to this use of num_pcidevs ? That might save someone some future confusion.

Here, c_info->passthrough is set to enabled if either d_config->num_pcidevs or d_config->num_dtdevs is set. Do you think we need to add there additional comment? If so can you please help with what should I write there?

BTW. None of the patch I'm preparing with regards to this discussion modifies libxl_create where c_info.passthrough i set. Do you still want me to add some comment there?

Quoted 9 lines

Yes, I think so. I won't insisting on it if you feel it doesn't make sense.

Maybe something like

// NB, on ARM, libxl__arch_blah directly examines num_pcidevs to // decide whether to enable vpci, rather than using c_info->passthrough. // This will be insufficient if and when ARM does PCI hotplug.

Well we are not enabling vpci. We are creating a DT node for it. So either I will write:
/*
* Note: libxl_arm directly examines num_pcidevs to decide whether
* to create a vPCI DT node, rather than using c_info->passthrough.
* This will be insufficient if and when ARM does PCI hotplug. */

or I will not add any comment (it can be add in the future when "enabling" vpci). @Julien?

Quoted 4 lines

?

Ian.

Cheers,
Michal

#187 lore raw

Re: [PATCH v6 3/3] arm/libxl: Emulated PCI device tree node in libxl [and 1 more messages]

Hi Michal,

Quoted 33 lines · Michal Orzel
Quoted 13 lines · Ian Jackson

Michal Orzel writes ("Re: [PATCH v6 3/3] arm/libxl: Emulated PCI device tree node in libxl [and 1 more messages]"):

Quoted 11 lines · Ian Jackson
Quoted 3 lines

Can you leave a comment somewhere (near where c_info.passthrough is set) pointing to this use of num_pcidevs ? That might save someone some future confusion.

Here, c_info->passthrough is set to enabled if either d_config->num_pcidevs or d_config->num_dtdevs is set. Do you think we need to add there additional comment? If so can you please help with what should I write there?

BTW. None of the patch I'm preparing with regards to this discussion modifies libxl_create where c_info.passthrough i set. Do you still want me to add some comment there?

Quoted 9 lines

Yes, I think so. I won't insisting on it if you feel it doesn't make sense.

Maybe something like

   // NB, on ARM, libxl__arch_blah directly examines num_pcidevs to
   // decide whether to enable vpci, rather than using c_info->passthrough.
   // This will be insufficient if and when ARM does PCI hotplug.

Well we are not enabling vpci. We are creating a DT node for it. So either I will write:
/*
* Note: libxl_arm directly examines num_pcidevs to decide whether
* to create a vPCI DT node, rather than using c_info->passthrough.
* This will be insufficient if and when ARM does PCI hotplug. */

Quoted 2 lines

or I will not add any comment (it can be add in the future when "enabling" vpci). @Julien?

I would prefer if we had a comment. Your proposal makes more sense as we only create the DT.

Long term, I would expect a similar check to be necessary to set the vCPI flag at the domain creation. So it would be best to introduce an internal field 'vpci' to avoid duplicating that check. (Note I am not requesting this change for Xen 4.16).

Cheers,

Signature · 1 line
Julien Grall
#189 lore raw

Re: [PATCH v6 3/3] arm/libxl: Emulated PCI device tree node in libxl [and 1 more messages]

In reply to Michal Orzel

Michal Orzel writes ("Re: [PATCH v6 3/3] arm/libxl: Emulated PCI device tree node in libxl [and 1 more messages]"):

Quoted 7 lines · Ian Jackson
Quoted 5 lines

Maybe something like

// NB, on ARM, libxl__arch_blah directly examines num_pcidevs to // decide whether to enable vpci, rather than using c_info->passthrough. // This will be insufficient if and when ARM does PCI hotplug.

Quoted 7 lines

Well we are not enabling vpci. We are creating a DT node for it. So either I will write:
/*
* Note: libxl_arm directly examines num_pcidevs to decide whether
* to create a vPCI DT node, rather than using c_info->passthrough.
* This will be insufficient if and when ARM does PCI hotplug. */

That sounds good to me. Thank you for correcting me :-).

Ian.

#154 lore raw

Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM.

In reply to Roger Pau Monné

Hi Roger,

Quoted 45 lines · Roger Pau Monné
Quoted 33 lines · Jan Beulich
Quoted 32 lines · Ian Jackson

Bertrand Marquis writes ("Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM."):

Quoted 12 lines
Quoted 11 lines · Jan Beulich

The latter is fine to be put here (i.e. FTAOD I'm fine with it staying here). For the former I even question its original placement in asm-x86/pci.h: It's not generally correct as per the PCI spec, as the bus portion of the address can be anywhere from 1 to 8 bits. And in fact there is a reason why this macro was/is used in only a single place, but not e.g. in x86'es handling of physical MCFG. It is merely an implementation choice in vPCI that the entire segment 0 has a linear address range covering all 256 buses. Hence I think this wants to move to xen/vpci.h and then perhaps also be named VPCI_ECAM_BDF().

Quoted 5 lines

On previous version it was request to renamed this to ECAM and agreed to put is here. Now you want me to rename it to VPCI and move it again. I would like to have a confirmation that this is ok and the final move if possible.

@Roger can you confirm this is what is wanted ?

I think Roger is not available today I'm afraid.

Bertrand, can you give me a link to the comment from Roger ? Assuming that it says what I think it will say:

I think the best thing to do will be to leave the name as it was in the most recent version of your series. I don't think it makes sense to block this patch over a naming disagreement. And it would be best to minimise unnecessary churn.

I would be happy to release-ack a name change (perhaps proposed bo Jan or Roger) supposing that that is the ultimate maintainer consensus.

Quoted 6 lines
Quoted 1 line

Jan, would that approach be OK with you ?

Well, yes, if a subsequent name change is okay, then I could live with that. I'd still find it odd to rename a function immediately after it already got renamed. As expressed elsewhere, I suspect in his request Roger did not pay attention to a use of the function in non-ECAM code.

Using MMCFG_BDF was original requested by Julien, not myself I think:

Quoted 7 lines

https://lore.kernel.org/xen-devel/[email protected]/

I'm slightly loss in so many messages. On x86 we subtract the MCFG start address from the passed one before getting the BDF, and then we add the startting bus address passed in the ACPI table. This is so far not need on Arm AFAICT because of the fixed nature of the selected virtual ECAM region.

At the end my patch will add in xen/pci.h:
#define ECAM_BDF(addr) (((addr) & 0x0ffff000) >> 12)
#define ECAM_REG_OFFSET(addr) ((addr) & 0x00000fff)

Now seeing the comment the question is should those be renamed with a VPCI prefix and be moved to xen/vpci.h.

So far ECAM_BDF is only used in vpci_mmcfg_decode_addr which is only called before calling vpci_ecam_{read/write}.

ECAM_REG_OFFSET is only used in arm vpci code.

Do you think the current state is ok of the renaming + moving should be done ?

Cheers
Bertrand

#188 lore raw

Re: [PATCH v6 3/3] arm/libxl: Emulated PCI device tree node in libxl [and 1 more messages]

In reply to Julien Grall

Julien Grall writes ("Re: [PATCH v6 3/3] arm/libxl: Emulated PCI device tree node in libxl [and 1 more messages]"):

Quoted 4 lines

Long term, I would expect a similar check to be necessary to set the vCPI flag at the domain creation. So it would be best to introduce an internal field 'vpci' to avoid duplicating that check. (Note I am not requesting this change for Xen 4.16).

Yes, I agree with all of that.

Ian.

#155 lore raw

Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM.

In reply to Bertrand Marquis
Quoted 49 lines · Bertrand Marquis

Hi Roger,

Quoted 47 lines · Roger Pau Monné
Quoted 33 lines · Jan Beulich
Quoted 32 lines · Ian Jackson

Bertrand Marquis writes ("Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM."):

Quoted 12 lines
Quoted 11 lines · Jan Beulich

The latter is fine to be put here (i.e. FTAOD I'm fine with it staying here). For the former I even question its original placement in asm-x86/pci.h: It's not generally correct as per the PCI spec, as the bus portion of the address can be anywhere from 1 to 8 bits. And in fact there is a reason why this macro was/is used in only a single place, but not e.g. in x86'es handling of physical MCFG. It is merely an implementation choice in vPCI that the entire segment 0 has a linear address range covering all 256 buses. Hence I think this wants to move to xen/vpci.h and then perhaps also be named VPCI_ECAM_BDF().

Quoted 5 lines

On previous version it was request to renamed this to ECAM and agreed to put is here. Now you want me to rename it to VPCI and move it again. I would like to have a confirmation that this is ok and the final move if possible.

@Roger can you confirm this is what is wanted ?

I think Roger is not available today I'm afraid.

Bertrand, can you give me a link to the comment from Roger ? Assuming that it says what I think it will say:

I think the best thing to do will be to leave the name as it was in the most recent version of your series. I don't think it makes sense to block this patch over a naming disagreement. And it would be best to minimise unnecessary churn.

I would be happy to release-ack a name change (perhaps proposed bo Jan or Roger) supposing that that is the ultimate maintainer consensus.

Quoted 6 lines
Quoted 1 line

Jan, would that approach be OK with you ?

Well, yes, if a subsequent name change is okay, then I could live with that. I'd still find it odd to rename a function immediately after it already got renamed. As expressed elsewhere, I suspect in his request Roger did not pay attention to a use of the function in non-ECAM code.

Using MMCFG_BDF was original requested by Julien, not myself I think:

https://lore.kernel.org/xen-devel/[email protected]/

Quoted 8 lines
Quoted 5 lines

I'm slightly loss in so many messages. On x86 we subtract the MCFG start address from the passed one before getting the BDF, and then we add the startting bus address passed in the ACPI table. This is so far not need on Arm AFAICT because of the fixed nature of the selected virtual ECAM region.

At the end my patch will add in xen/pci.h:
#define ECAM_BDF(addr) (((addr) & 0x0ffff000) >> 12)

Since you still make this proposal, once again: I'm not going to accept such a macro in this header, whatever the name. Its prior placement was wrong as well. Only ...

Quoted 1 line

#define ECAM_REG_OFFSET(addr) ((addr) & 0x00000fff)

... this one is fine to live here (and presumably it could gain uses elsewhere).

Jan

Quoted 13 lines

Now seeing the comment the question is should those be renamed with a VPCI prefix and be moved to xen/vpci.h.

So far ECAM_BDF is only used in vpci_mmcfg_decode_addr which is only called before calling vpci_ecam_{read/write}.

ECAM_REG_OFFSET is only used in arm vpci code.

Do you think the current state is ok of the renaming + moving should be done ?

Cheers
Bertrand

#156 lore raw

Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM.

Hi Jan,

Quoted 61 lines · Jan Beulich
Quoted 49 lines · Bertrand Marquis

Hi Roger,

Quoted 47 lines · Roger Pau Monné
Quoted 33 lines · Jan Beulich
Quoted 32 lines · Ian Jackson

Bertrand Marquis writes ("Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM."):

Quoted 12 lines
Quoted 11 lines · Jan Beulich

The latter is fine to be put here (i.e. FTAOD I'm fine with it staying here). For the former I even question its original placement in asm-x86/pci.h: It's not generally correct as per the PCI spec, as the bus portion of the address can be anywhere from 1 to 8 bits. And in fact there is a reason why this macro was/is used in only a single place, but not e.g. in x86'es handling of physical MCFG. It is merely an implementation choice in vPCI that the entire segment 0 has a linear address range covering all 256 buses. Hence I think this wants to move to xen/vpci.h and then perhaps also be named VPCI_ECAM_BDF().

Quoted 5 lines

On previous version it was request to renamed this to ECAM and agreed to put is here. Now you want me to rename it to VPCI and move it again. I would like to have a confirmation that this is ok and the final move if possible.

@Roger can you confirm this is what is wanted ?

I think Roger is not available today I'm afraid.

Bertrand, can you give me a link to the comment from Roger ? Assuming that it says what I think it will say:

I think the best thing to do will be to leave the name as it was in the most recent version of your series. I don't think it makes sense to block this patch over a naming disagreement. And it would be best to minimise unnecessary churn.

I would be happy to release-ack a name change (perhaps proposed bo Jan or Roger) supposing that that is the ultimate maintainer consensus.

Quoted 6 lines
Quoted 1 line

Jan, would that approach be OK with you ?

Well, yes, if a subsequent name change is okay, then I could live with that. I'd still find it odd to rename a function immediately after it already got renamed. As expressed elsewhere, I suspect in his request Roger did not pay attention to a use of the function in non-ECAM code.

Using MMCFG_BDF was original requested by Julien, not myself I think:

https://lore.kernel.org/xen-devel/[email protected]/

Quoted 8 lines
Quoted 5 lines

I'm slightly loss in so many messages. On x86 we subtract the MCFG start address from the passed one before getting the BDF, and then we add the startting bus address passed in the ACPI table. This is so far not need on Arm AFAICT because of the fixed nature of the selected virtual ECAM region.

At the end my patch will add in xen/pci.h:
#define ECAM_BDF(addr) (((addr) & 0x0ffff000) >> 12)

Quoted 8 lines

Since you still make this proposal, once again: I'm not going to accept such a macro in this header, whatever the name. Its prior placement was wrong as well. Only ...

Quoted 1 line

#define ECAM_REG_OFFSET(addr) ((addr) & 0x00000fff)

... this one is fine to live here (and presumably it could gain uses elsewhere).

So you would agree if they are both moved to vpci.h with a VPCI_ prefix ?

Bertrand

Quoted 15 lines

Jan

Quoted 12 lines

Now seeing the comment the question is should those be renamed with a VPCI prefix and be moved to xen/vpci.h.

So far ECAM_BDF is only used in vpci_mmcfg_decode_addr which is only called before calling vpci_ecam_{read/write}.

ECAM_REG_OFFSET is only used in arm vpci code.

Do you think the current state is ok of the renaming + moving should be done ?

Cheers
Bertrand

#157 lore raw

Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM.

Quoted 61 lines · Bertrand Marquis
Quoted 61 lines · Jan Beulich
Quoted 46 lines · Bertrand Marquis
Quoted 46 lines · Roger Pau Monné
Quoted 33 lines · Jan Beulich
Quoted 32 lines · Ian Jackson

Bertrand Marquis writes ("Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM."):

Quoted 12 lines
Quoted 11 lines · Jan Beulich

The latter is fine to be put here (i.e. FTAOD I'm fine with it staying here). For the former I even question its original placement in asm-x86/pci.h: It's not generally correct as per the PCI spec, as the bus portion of the address can be anywhere from 1 to 8 bits. And in fact there is a reason why this macro was/is used in only a single place, but not e.g. in x86'es handling of physical MCFG. It is merely an implementation choice in vPCI that the entire segment 0 has a linear address range covering all 256 buses. Hence I think this wants to move to xen/vpci.h and then perhaps also be named VPCI_ECAM_BDF().

Quoted 5 lines

On previous version it was request to renamed this to ECAM and agreed to put is here. Now you want me to rename it to VPCI and move it again. I would like to have a confirmation that this is ok and the final move if possible.

@Roger can you confirm this is what is wanted ?

I think Roger is not available today I'm afraid.

Bertrand, can you give me a link to the comment from Roger ? Assuming that it says what I think it will say:

I think the best thing to do will be to leave the name as it was in the most recent version of your series. I don't think it makes sense to block this patch over a naming disagreement. And it would be best to minimise unnecessary churn.

I would be happy to release-ack a name change (perhaps proposed bo Jan or Roger) supposing that that is the ultimate maintainer consensus.

Quoted 6 lines
Quoted 1 line

Jan, would that approach be OK with you ?

Well, yes, if a subsequent name change is okay, then I could live with that. I'd still find it odd to rename a function immediately after it already got renamed. As expressed elsewhere, I suspect in his request Roger did not pay attention to a use of the function in non-ECAM code.

Using MMCFG_BDF was original requested by Julien, not myself I think:

https://lore.kernel.org/xen-devel/[email protected]/

Quoted 8 lines
Quoted 5 lines

I'm slightly loss in so many messages. On x86 we subtract the MCFG start address from the passed one before getting the BDF, and then we add the startting bus address passed in the ACPI table. This is so far not need on Arm AFAICT because of the fixed nature of the selected virtual ECAM region.

At the end my patch will add in xen/pci.h:
#define ECAM_BDF(addr) (((addr) & 0x0ffff000) >> 12)

Since you still make this proposal, once again: I'm not going to accept such a macro in this header, whatever the name. Its prior placement was wrong as well. Only ...

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

#define ECAM_REG_OFFSET(addr) ((addr) & 0x00000fff)

... this one is fine to live here (and presumably it could gain uses elsewhere).

So you would agree if they are both moved to vpci.h with a VPCI_ prefix ?

I wouldn't object, but as said several times before I see no reason to also move and rename ECAM_REG_OFFSET(). If you moved it and if later we find a use for it outside of vPCI, we'd need to rename and move it again.

Jan

#158 lore raw

Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM.

Hi Jan,

Quoted 69 lines · Jan Beulich
Quoted 66 lines · Bertrand Marquis
Quoted 65 lines · Jan Beulich
Quoted 46 lines · Bertrand Marquis
Quoted 46 lines · Roger Pau Monné
Quoted 33 lines · Jan Beulich
Quoted 32 lines · Ian Jackson

Bertrand Marquis writes ("Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM."):

Quoted 12 lines
Quoted 11 lines · Jan Beulich

The latter is fine to be put here (i.e. FTAOD I'm fine with it staying here). For the former I even question its original placement in asm-x86/pci.h: It's not generally correct as per the PCI spec, as the bus portion of the address can be anywhere from 1 to 8 bits. And in fact there is a reason why this macro was/is used in only a single place, but not e.g. in x86'es handling of physical MCFG. It is merely an implementation choice in vPCI that the entire segment 0 has a linear address range covering all 256 buses. Hence I think this wants to move to xen/vpci.h and then perhaps also be named VPCI_ECAM_BDF().

Quoted 5 lines

On previous version it was request to renamed this to ECAM and agreed to put is here. Now you want me to rename it to VPCI and move it again. I would like to have a confirmation that this is ok and the final move if possible.

@Roger can you confirm this is what is wanted ?

I think Roger is not available today I'm afraid.

Bertrand, can you give me a link to the comment from Roger ? Assuming that it says what I think it will say:

I think the best thing to do will be to leave the name as it was in the most recent version of your series. I don't think it makes sense to block this patch over a naming disagreement. And it would be best to minimise unnecessary churn.

I would be happy to release-ack a name change (perhaps proposed bo Jan or Roger) supposing that that is the ultimate maintainer consensus.

Quoted 6 lines
Quoted 1 line

Jan, would that approach be OK with you ?

Well, yes, if a subsequent name change is okay, then I could live with that. I'd still find it odd to rename a function immediately after it already got renamed. As expressed elsewhere, I suspect in his request Roger did not pay attention to a use of the function in non-ECAM code.

Using MMCFG_BDF was original requested by Julien, not myself I think:

https://lore.kernel.org/xen-devel/[email protected]/

Quoted 8 lines
Quoted 5 lines

I'm slightly loss in so many messages. On x86 we subtract the MCFG start address from the passed one before getting the BDF, and then we add the startting bus address passed in the ACPI table. This is so far not need on Arm AFAICT because of the fixed nature of the selected virtual ECAM region.

At the end my patch will add in xen/pci.h:
#define ECAM_BDF(addr) (((addr) & 0x0ffff000) >> 12)

Since you still make this proposal, once again: I'm not going to accept such a macro in this header, whatever the name. Its prior placement was wrong as well. Only ...

Quoted 1 line

#define ECAM_REG_OFFSET(addr) ((addr) & 0x00000fff)

... this one is fine to live here (and presumably it could gain uses elsewhere).

Quoted 6 lines
Quoted 1 line

So you would agree if they are both moved to vpci.h with a VPCI_ prefix ?

I wouldn't object, but as said several times before I see no reason to also move and rename ECAM_REG_OFFSET(). If you moved it and if later we find a use for it outside of vPCI, we'd need to rename and move it again.

I will move BDF to vpci.h and had VPCI prefix and keep REG_OFFSET as it is and where it is then.

Cheers
Bertrand

Quoted 3 lines

Jan

#173 lore raw

Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM.

In reply to Bertrand Marquis
Quoted 56 lines · Bertrand Marquis

Hi Jan,

Quoted 44 lines · Jan Beulich
Quoted 41 lines · Bertrand Marquis
Quoted 29 lines · Jan Beulich
Quoted 22 lines · Bertrand Marquis
Quoted 6 lines · Roger Pau Monné
Quoted 4 lines · Bertrand Marquis
(quoted diff) +0 -0
@@ -752,6 +752,19 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
 
  check_pdev(pdev);
 
Quoted 15 lines

Review int pci_add_device(u16 seg, u8 bus, u8 devfn, · line 766

Quoted 12 lines
(quoted diff) +12 -0
+#ifdef CONFIG_ARM
+    /*
+     * On ARM PCI devices discovery will be done by Dom0. Add vpci handler when
+     * Dom0 inform XEN to add the PCI devices in XEN.
+     */
+    ret = vpci_add_handlers(pdev);
+    if ( ret )
+    {
+        printk(XENLOG_ERR "Setup of vPCI failed: %d\n", ret);
+        goto out;
+    }
+#endif

I think vpci_add_handlers should be called after checking that pdev->domain is != NULL, so I would move this chunk a bit below.

Quoted 4 lines

On arm this would prevent the dom0less use case or to have the PCI bus enumerated from an other domain.
@oleksandr: can you comment on this one, you might have a better answer than me on this ?

Quoted 6 lines

Well, without Xen doing the enumeration, some other entity would need to do so, including the reporting to Xen. Obviously without a Dom0 it would be ambiguous which domain to assign the device to; perhaps it should be the caller in this case? That would make that caller domain a pseudo-hwdom though, as far as PCI is concerned, which may not be desirable according to my (limited) understanding of dom0less.

This is not really related to this patch but the plan is the following:
- enumeration would have to be done by the firmware or boot loader before
- xen will have some code to detect PCI devices
- dom0less can be used to assign PCI devices to guest

Quoted 5 lines
Quoted 2 lines

Anyway does not change the fact that this must be called when domain is not NULL and I will fix that.

Since we now all seem to agree that the NULL would have been a problem, may I ask in how far any of this has actually been tested?

With the whole serie currently on gitlab we have extensively tested passing through PCI devices on Arm in several configuration (number of device, MSI, MSI-X) and check that PCI was still functional on x86.

Quoted 5 lines

With the patches pushed to Xen right now it was checked that:
- xen compiles properly on arm32, arm64 and x86
- xen compiles properly with VPCI activated (using a patch) on arm32 and arm64
- xen on x86 is functionnal (using basic test on QEMU)
- xen on arm64 is functionnal (with some extensive tests on different targets)

I thinks it's unlikely, but since I haven't checked myself, could you see if the vpci user-space test harness (tools/tests/vpci) still builds and functions properly?

Thanks, Roger.

#174 lore raw

Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM.

Quoted 67 lines · Roger Pau Monné
Quoted 56 lines · Bertrand Marquis

Hi Jan,

Quoted 44 lines · Jan Beulich
Quoted 41 lines · Bertrand Marquis
Quoted 29 lines · Jan Beulich
Quoted 22 lines · Bertrand Marquis
Quoted 6 lines · Roger Pau Monné
Quoted 4 lines · Bertrand Marquis
(quoted diff) +0 -0
@@ -752,6 +752,19 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
 
 check_pdev(pdev);
 
Quoted 15 lines

Review int pci_add_device(u16 seg, u8 bus, u8 devfn, · line 766

Quoted 12 lines
(quoted diff) +12 -0
+#ifdef CONFIG_ARM
+    /*
+     * On ARM PCI devices discovery will be done by Dom0. Add vpci handler when
+     * Dom0 inform XEN to add the PCI devices in XEN.
+     */
+    ret = vpci_add_handlers(pdev);
+    if ( ret )
+    {
+        printk(XENLOG_ERR "Setup of vPCI failed: %d\n", ret);
+        goto out;
+    }
+#endif

I think vpci_add_handlers should be called after checking that pdev->domain is != NULL, so I would move this chunk a bit below.

Quoted 4 lines

On arm this would prevent the dom0less use case or to have the PCI bus enumerated from an other domain.
@oleksandr: can you comment on this one, you might have a better answer than me on this ?

Quoted 6 lines

Well, without Xen doing the enumeration, some other entity would need to do so, including the reporting to Xen. Obviously without a Dom0 it would be ambiguous which domain to assign the device to; perhaps it should be the caller in this case? That would make that caller domain a pseudo-hwdom though, as far as PCI is concerned, which may not be desirable according to my (limited) understanding of dom0less.

This is not really related to this patch but the plan is the following:
- enumeration would have to be done by the firmware or boot loader before
- xen will have some code to detect PCI devices
- dom0less can be used to assign PCI devices to guest

Quoted 5 lines
Quoted 2 lines

Anyway does not change the fact that this must be called when domain is not NULL and I will fix that.

Since we now all seem to agree that the NULL would have been a problem, may I ask in how far any of this has actually been tested?

With the whole serie currently on gitlab we have extensively tested passing through PCI devices on Arm in several configuration (number of device, MSI, MSI-X) and check that PCI was still functional on x86.

Quoted 5 lines

With the patches pushed to Xen right now it was checked that:
- xen compiles properly on arm32, arm64 and x86
- xen compiles properly with VPCI activated (using a patch) on arm32 and arm64
- xen on x86 is functionnal (using basic test on QEMU)
- xen on arm64 is functionnal (with some extensive tests on different targets)

Quoted 3 lines

I thinks it's unlikely, but since I haven't checked myself, could you see if the vpci user-space test harness (tools/tests/vpci) still builds and functions properly?

Hi Roger,

Bertrand is going to send the v7, however I’m keen to do this test but unfortunately I’ve never used it before and I don’t have an x86 environment, do you have any resource that I can read or some guidance?

Cheers,
Luca

Quoted 3 lines

Thanks, Roger.

#175 lore raw

Re: [PATCH v6 2/3] xen/arm: Enable the existing x86 virtual PCI support for ARM.

Quoted 71 lines · Luca Fancellu
Quoted 67 lines · Roger Pau Monné
Quoted 56 lines · Bertrand Marquis

Hi Jan,

Quoted 44 lines · Jan Beulich
Quoted 41 lines · Bertrand Marquis
Quoted 29 lines · Jan Beulich
Quoted 22 lines · Bertrand Marquis
Quoted 6 lines · Roger Pau Monné
Quoted 4 lines · Bertrand Marquis
(quoted diff) +0 -0
@@ -752,6 +752,19 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
 
 check_pdev(pdev);
 
Quoted 15 lines

Review int pci_add_device(u16 seg, u8 bus, u8 devfn, · line 766

Quoted 12 lines
(quoted diff) +12 -0
+#ifdef CONFIG_ARM
+    /*
+     * On ARM PCI devices discovery will be done by Dom0. Add vpci handler when
+     * Dom0 inform XEN to add the PCI devices in XEN.
+     */
+    ret = vpci_add_handlers(pdev);
+    if ( ret )
+    {
+        printk(XENLOG_ERR "Setup of vPCI failed: %d\n", ret);
+        goto out;
+    }
+#endif

I think vpci_add_handlers should be called after checking that pdev->domain is != NULL, so I would move this chunk a bit below.

Quoted 4 lines

On arm this would prevent the dom0less use case or to have the PCI bus enumerated from an other domain.
@oleksandr: can you comment on this one, you might have a better answer than me on this ?

Quoted 6 lines

Well, without Xen doing the enumeration, some other entity would need to do so, including the reporting to Xen. Obviously without a Dom0 it would be ambiguous which domain to assign the device to; perhaps it should be the caller in this case? That would make that caller domain a pseudo-hwdom though, as far as PCI is concerned, which may not be desirable according to my (limited) understanding of dom0less.

This is not really related to this patch but the plan is the following:
- enumeration would have to be done by the firmware or boot loader before
- xen will have some code to detect PCI devices
- dom0less can be used to assign PCI devices to guest

Quoted 5 lines
Quoted 2 lines

Anyway does not change the fact that this must be called when domain is not NULL and I will fix that.

Since we now all seem to agree that the NULL would have been a problem, may I ask in how far any of this has actually been tested?

With the whole serie currently on gitlab we have extensively tested passing through PCI devices on Arm in several configuration (number of device, MSI, MSI-X) and check that PCI was still functional on x86.

Quoted 5 lines

With the patches pushed to Xen right now it was checked that:
- xen compiles properly on arm32, arm64 and x86
- xen compiles properly with VPCI activated (using a patch) on arm32 and arm64
- xen on x86 is functionnal (using basic test on QEMU)
- xen on arm64 is functionnal (with some extensive tests on different targets)

Quoted 3 lines

I thinks it's unlikely, but since I haven't checked myself, could you see if the vpci user-space test harness (tools/tests/vpci) still builds and functions properly?

Quoted 5 lines

Hi Roger,

Bertrand is going to send the v7, however I’m keen to do this test but unfortunately I’ve never used it before and I don’t have an x86 environment, do you have any resource that I can read or some guidance?

Oh, you just need to build it (make) and then run it (make run). If it returns success everything is fine.

Regards, Roger.

#33 lore raw

xen/arm: Missing appropriate locking for the IOMMU (WAS Re: [PATCH v5 02/11] xen/arm: Add PHYSDEVOP_pci_device_(*add/remove) support for ARM)

In reply to Rahul Singh

Hi all,

While going through the passthrough code. I noticed that we don't have a common lock for the IOMMU between the PCI and DT code.

This is going to be an issue given it would technically be possible to add a PCI device while assigning a DT.

Rahul, Bertrand, Oleksandr, can you have a look at the issue?

Cheers,

Quoted 274 lines · Rahul Singh

Hardware domain is in charge of doing the PCI enumeration and will discover the PCI devices and then will communicate to XEN via hyper call PHYSDEVOP_pci_device_add(..) to add the PCI devices in XEN.

Also implement PHYSDEVOP_pci_device_remove(..) to remove the PCI device.

As most of the code for PHYSDEVOP_pci_device_* is the same between x86 and ARM, move the code to a common file to avoid duplication.

There are other PHYSDEVOP_pci_device_* operations to add PCI devices. Currently implemented PHYSDEVOP_pci_device_remove(..) and PHYSDEVOP_pci_device_add(..) only as those are minimum required to support PCI passthrough on ARM.

---
Change in v5:
- Move the pci_physdev_op() stub to xen/arch/arm/physdev.c. Change in v4:
- Move file commom/physdev.c to drivers/pci/physdev.c
- minor comments.
Change in v3: Fixed minor comment.
Change in v2:
- Add support for PHYSDEVOP_pci_device_remove()
- Move code to common code
---

---
  xen/arch/arm/physdev.c        |  6 ++-
  xen/arch/x86/physdev.c        | 52 +----------------------
  xen/arch/x86/x86_64/physdev.c |  2 +-
  xen/drivers/pci/Makefile      |  1 +
  xen/drivers/pci/physdev.c     | 80 +++++++++++++++++++++++++++++++++++
  xen/include/public/arch-arm.h |  4 +-
  xen/include/xen/hypercall.h   |  4 ++
  7 files changed, 96 insertions(+), 53 deletions(-)

create mode 100644 xen/drivers/pci/physdev.c

xen/arch/arm/physdev.c +5 -1
index e91355fe22..f9aa274dda 100644
@@ -8,13 +8,17 @@
  #include <xen/lib.h>
  #include <xen/errno.h>
  #include <xen/sched.h>
-#include <asm/hypercall.h>
+#include <xen/hypercall.h>
 
 
  int do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
  {
+#ifdef CONFIG_HAS_PCI
+    return pci_physdev_op(cmd, arg);
+#else
      gdprintk(XENLOG_DEBUG, "PHYSDEVOP cmd=%d: not implemented\n", cmd);
      return -ENOSYS;
+#endif
  }
 
  /*
xen/arch/x86/physdev.c +2 -50
index 23465bcd00..ea38be8b79 100644
@@ -12,7 +12,7 @@
  #include <asm/io_apic.h>
  #include <asm/msi.h>
  #include <asm/hvm/irq.h>
-#include <asm/hypercall.h>
+#include <xen/hypercall.h>
  #include <public/xen.h>
  #include <public/physdev.h>
  #include <xsm/xsm.h>
@@ -480,54 +480,6 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
          break;
      }
 
-    case PHYSDEVOP_pci_device_add: {
-        struct physdev_pci_device_add add;
-        struct pci_dev_info pdev_info;
-        nodeid_t node;
-
-        ret = -EFAULT;
-        if ( copy_from_guest(&add, arg, 1) != 0 )
-            break;
-
-        pdev_info.is_extfn = !!(add.flags & XEN_PCI_DEV_EXTFN);
-        if ( add.flags & XEN_PCI_DEV_VIRTFN )
-        {
-            pdev_info.is_virtfn = 1;
-            pdev_info.physfn.bus = add.physfn.bus;
-            pdev_info.physfn.devfn = add.physfn.devfn;
-        }
-        else
-            pdev_info.is_virtfn = 0;
-
-        if ( add.flags & XEN_PCI_DEV_PXM )
-        {
-            uint32_t pxm;
-            size_t optarr_off = offsetof(struct physdev_pci_device_add, optarr) /
-                                sizeof(add.optarr[0]);
-
-            if ( copy_from_guest_offset(&pxm, arg, optarr_off, 1) )
-                break;
-
-            node = pxm_to_node(pxm);
-        }
-        else
-            node = NUMA_NO_NODE;
-
-        ret = pci_add_device(add.seg, add.bus, add.devfn, &pdev_info, node);
-        break;
-    }
-
-    case PHYSDEVOP_pci_device_remove: {
-        struct physdev_pci_device dev;
-
-        ret = -EFAULT;
-        if ( copy_from_guest(&dev, arg, 1) != 0 )
-            break;
-
-        ret = pci_remove_device(dev.seg, dev.bus, dev.devfn);
-        break;
-    }
-
      case PHYSDEVOP_prepare_msix:
      case PHYSDEVOP_release_msix: {
          struct physdev_pci_device dev;
@@ -663,7 +615,7 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
      }
 
      default:
-        ret = -ENOSYS;
+        ret = pci_physdev_op(cmd, arg);
          break;
      }
 
xen/arch/x86/x86_64/physdev.c +1 -1
index 0a50cbd4d8..e3cbd5ebcb 100644
@@ -9,7 +9,7 @@ EMIT_FILE;
  #include <compat/xen.h>
  #include <compat/event_channel.h>
  #include <compat/physdev.h>
-#include <asm/hypercall.h>
+#include <xen/hypercall.h>
 
  #define do_physdev_op compat_physdev_op
 
xen/drivers/pci/Makefile +1 -0
index a98035df4c..972c923db0 100644
@@ -1 +1,2 @@
  obj-y += pci.o
+obj-y += physdev.o
xen/drivers/pci/physdev.c new file +80 -0
new file mode 100644
index 0000000000..4f3e1a96c0
@@ -0,0 +1,80 @@
+
+#include <xen/guest_access.h>
+#include <xen/hypercall.h>
+#include <xen/init.h>
+
+#ifndef COMPAT
+typedef long ret_t;
+#endif
+
+ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
+{
+    ret_t ret;
+
+    switch ( cmd )
+    {
+    case PHYSDEVOP_pci_device_add: {
+        struct physdev_pci_device_add add;
+        struct pci_dev_info pdev_info;
+        nodeid_t node = NUMA_NO_NODE;
+
+        ret = -EFAULT;
+        if ( copy_from_guest(&add, arg, 1) != 0 )
+            break;
+
+        pdev_info.is_extfn = (add.flags & XEN_PCI_DEV_EXTFN);
+        if ( add.flags & XEN_PCI_DEV_VIRTFN )
+        {
+            pdev_info.is_virtfn = true;
+            pdev_info.physfn.bus = add.physfn.bus;
+            pdev_info.physfn.devfn = add.physfn.devfn;
+        }
+        else
+            pdev_info.is_virtfn = false;
+
+#ifdef CONFIG_NUMA
+        if ( add.flags & XEN_PCI_DEV_PXM )
+        {
+            uint32_t pxm;
+            size_t optarr_off = offsetof(struct physdev_pci_device_add, optarr) /
+                                sizeof(add.optarr[0]);
+
+            if ( copy_from_guest_offset(&pxm, arg, optarr_off, 1) )
+                break;
+
+            node = pxm_to_node(pxm);
+        }
+#endif
+
+        ret = pci_add_device(add.seg, add.bus, add.devfn, &pdev_info, node);
+        break;
+    }
+
+    case PHYSDEVOP_pci_device_remove: {
+        struct physdev_pci_device dev;
+
+        ret = -EFAULT;
+        if ( copy_from_guest(&dev, arg, 1) != 0 )
+            break;
+
+        ret = pci_remove_device(dev.seg, dev.bus, dev.devfn);
+        break;
+    }
+
+    default:
+        ret = -ENOSYS;
+        break;
+    }
+
+    return ret;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/include/public/arch-arm.h +3 -1
index 6b5a5f818a..d46c61fca9 100644
@@ -107,7 +107,9 @@
   *   All generic sub-operations
   *
   *  HYPERVISOR_physdev_op
- *   No sub-operations are currenty supported
+ *   Exactly these sub-operations are supported:
+ *   PHYSDEVOP_pci_device_add
+ *   PHYSDEVOP_pci_device_remove
   *
   *  HYPERVISOR_sysctl
   *   All generic sub-operations, with the exception of:
xen/include/xen/hypercall.h +4 -0
index 3771487a30..07b10ec230 100644
@@ -45,6 +45,10 @@ extern long
  do_platform_op(
      XEN_GUEST_HANDLE_PARAM(xen_platform_op_t) u_xenpf_op);
 
+extern long
+pci_physdev_op(
+    int cmd, XEN_GUEST_HANDLE_PARAM(void) arg);
+
  /*
   * To allow safe resume of do_memory_op() after preemption, we need to know
   * at what point in the page list to resume. For this purpose I steal the
Signature · 1 line
Julien Grall
#34 lore raw

Re: xen/arm: Missing appropriate locking for the IOMMU (WAS Re: [PATCH v5 02/11] xen/arm: Add PHYSDEVOP_pci_device_(*add/remove) support for ARM)

Hi Julien,

Quoted 9 lines · Julien Grall

Hi all,

While going through the passthrough code. I noticed that we don't have a common lock for the IOMMU between the PCI and DT code.

This is going to be an issue given it would technically be possible to add a PCI device while assigning a DT.

Rahul, Bertrand, Oleksandr, can you have a look at the issue?

Yes we can have a look at this.

Right now pci device add is done by dom0 so I do not think we have an issue in practice unless I wrongly understood something. But for sure in theory yes we need to look at this.

Cheers
Bertrand

Quoted 266 lines

Cheers,

Quoted 259 lines · Rahul Singh

Hardware domain is in charge of doing the PCI enumeration and will discover the PCI devices and then will communicate to XEN via hyper call PHYSDEVOP_pci_device_add(..) to add the PCI devices in XEN. Also implement PHYSDEVOP_pci_device_remove(..) to remove the PCI device. As most of the code for PHYSDEVOP_pci_device_* is the same between x86 and ARM, move the code to a common file to avoid duplication. There are other PHYSDEVOP_pci_device_* operations to add PCI devices. Currently implemented PHYSDEVOP_pci_device_remove(..) and PHYSDEVOP_pci_device_add(..) only as those are minimum required to support PCI passthrough on ARM.

---
Change in v5:
- Move the pci_physdev_op() stub to xen/arch/arm/physdev.c. Change in v4:
- Move file commom/physdev.c to drivers/pci/physdev.c
- minor comments.
Change in v3: Fixed minor comment.
Change in v2:
- Add support for PHYSDEVOP_pci_device_remove()
- Move code to common code
---

---
 xen/arch/arm/physdev.c        |  6 ++-
 xen/arch/x86/physdev.c        | 52 +----------------------
 xen/arch/x86/x86_64/physdev.c |  2 +-
 xen/drivers/pci/Makefile      |  1 +
 xen/drivers/pci/physdev.c     | 80 +++++++++++++++++++++++++++++++++++
 xen/include/public/arch-arm.h |  4 +-
 xen/include/xen/hypercall.h   |  4 ++
 7 files changed, 96 insertions(+), 53 deletions(-)
 create mode 100644 xen/drivers/pci/physdev.c
xen/arch/arm/physdev.c +5 -1
index e91355fe22..f9aa274dda 100644
@@ -8,13 +8,17 @@
 #include <xen/lib.h>
 #include <xen/errno.h>
 #include <xen/sched.h>
-#include <asm/hypercall.h>
+#include <xen/hypercall.h>
     int do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
 {
+#ifdef CONFIG_HAS_PCI
+    return pci_physdev_op(cmd, arg);
+#else
     gdprintk(XENLOG_DEBUG, "PHYSDEVOP cmd=%d: not implemented\n", cmd);
     return -ENOSYS;
+#endif
 }
   /*
xen/arch/x86/physdev.c +2 -49
index 23465bcd00..ea38be8b79 100644
@@ -12,7 +12,7 @@
 #include <asm/io_apic.h>
 #include <asm/msi.h>
 #include <asm/hvm/irq.h>
-#include <asm/hypercall.h>
+#include <xen/hypercall.h>
 #include <public/xen.h>
 #include <public/physdev.h>
 #include <xsm/xsm.h>
@@ -480,54 +480,6 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         break;
     }
 -    case PHYSDEVOP_pci_device_add: {
-        struct physdev_pci_device_add add;
-        struct pci_dev_info pdev_info;
-        nodeid_t node;
-
-        ret = -EFAULT;
-        if ( copy_from_guest(&add, arg, 1) != 0 )
-            break;
-
-        pdev_info.is_extfn = !!(add.flags & XEN_PCI_DEV_EXTFN);
-        if ( add.flags & XEN_PCI_DEV_VIRTFN )
-        {
-            pdev_info.is_virtfn = 1;
-            pdev_info.physfn.bus = add.physfn.bus;
-            pdev_info.physfn.devfn = add.physfn.devfn;
-        }
-        else
-            pdev_info.is_virtfn = 0;
-
-        if ( add.flags & XEN_PCI_DEV_PXM )
-        {
-            uint32_t pxm;
-            size_t optarr_off = offsetof(struct physdev_pci_device_add, optarr) /
-                                sizeof(add.optarr[0]);
-
-            if ( copy_from_guest_offset(&pxm, arg, optarr_off, 1) )
-                break;
-
-            node = pxm_to_node(pxm);
-        }
-        else
-            node = NUMA_NO_NODE;
-
-        ret = pci_add_device(add.seg, add.bus, add.devfn, &pdev_info, node);
-        break;
-    }
-
-    case PHYSDEVOP_pci_device_remove: {
-        struct physdev_pci_device dev;
-
-        ret = -EFAULT;
-        if ( copy_from_guest(&dev, arg, 1) != 0 )
-            break;
-
-        ret = pci_remove_device(dev.seg, dev.bus, dev.devfn);
-        break;
-    }
-
     case PHYSDEVOP_prepare_msix:
     case PHYSDEVOP_release_msix: {
         struct physdev_pci_device dev;
@@ -663,7 +615,7 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
     }
       default:
-        ret = -ENOSYS;
+        ret = pci_physdev_op(cmd, arg);
         break;
     }
 diff --git a/xen/arch/x86/x86_64/physdev.c b/xen/arch/x86/x86_64/physdev.c

index 0a50cbd4d8..e3cbd5ebcb 100644

xen/arch/x86/x86_64/physdev.c +1 -1
@@ -9,7 +9,7 @@ EMIT_FILE;
 #include <compat/xen.h>
 #include <compat/event_channel.h>
 #include <compat/physdev.h>
-#include <asm/hypercall.h>
+#include <xen/hypercall.h>
   #define do_physdev_op compat_physdev_op
 diff --git a/xen/drivers/pci/Makefile b/xen/drivers/pci/Makefile

index a98035df4c..972c923db0 100644

xen/drivers/pci/Makefile +1 -0
@@ -1 +1,2 @@
 obj-y += pci.o
+obj-y += physdev.o
xen/drivers/pci/physdev.c new file +80 -0
new file mode 100644
index 0000000000..4f3e1a96c0
@@ -0,0 +1,80 @@
+
+#include <xen/guest_access.h>
+#include <xen/hypercall.h>
+#include <xen/init.h>
+
+#ifndef COMPAT
+typedef long ret_t;
+#endif
+
+ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
+{
+    ret_t ret;
+
+    switch ( cmd )
+    {
+    case PHYSDEVOP_pci_device_add: {
+        struct physdev_pci_device_add add;
+        struct pci_dev_info pdev_info;
+        nodeid_t node = NUMA_NO_NODE;
+
+        ret = -EFAULT;
+        if ( copy_from_guest(&add, arg, 1) != 0 )
+            break;
+
+        pdev_info.is_extfn = (add.flags & XEN_PCI_DEV_EXTFN);
+        if ( add.flags & XEN_PCI_DEV_VIRTFN )
+        {
+            pdev_info.is_virtfn = true;
+            pdev_info.physfn.bus = add.physfn.bus;
+            pdev_info.physfn.devfn = add.physfn.devfn;
+        }
+        else
+            pdev_info.is_virtfn = false;
+
+#ifdef CONFIG_NUMA
+        if ( add.flags & XEN_PCI_DEV_PXM )
+        {
+            uint32_t pxm;
+            size_t optarr_off = offsetof(struct physdev_pci_device_add, optarr) /
+                                sizeof(add.optarr[0]);
+
+            if ( copy_from_guest_offset(&pxm, arg, optarr_off, 1) )
+                break;
+
+            node = pxm_to_node(pxm);
+        }
+#endif
+
+        ret = pci_add_device(add.seg, add.bus, add.devfn, &pdev_info, node);
+        break;
+    }
+
+    case PHYSDEVOP_pci_device_remove: {
+        struct physdev_pci_device dev;
+
+        ret = -EFAULT;
+        if ( copy_from_guest(&dev, arg, 1) != 0 )
+            break;
+
+        ret = pci_remove_device(dev.seg, dev.bus, dev.devfn);
+        break;
+    }
+
+    default:
+        ret = -ENOSYS;
+        break;
+    }
+
+    return ret;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/include/public/arch-arm.h +3 -1
index 6b5a5f818a..d46c61fca9 100644
@@ -107,7 +107,9 @@
  *   All generic sub-operations
  *
  *  HYPERVISOR_physdev_op
- *   No sub-operations are currenty supported
+ *   Exactly these sub-operations are supported:
+ *   PHYSDEVOP_pci_device_add
+ *   PHYSDEVOP_pci_device_remove
  *
  *  HYPERVISOR_sysctl
  *   All generic sub-operations, with the exception of:
xen/include/xen/hypercall.h +3 -0
index 3771487a30..07b10ec230 100644
@@ -45,6 +45,10 @@ extern long
 do_platform_op(
     XEN_GUEST_HANDLE_PARAM(xen_platform_op_t) u_xenpf_op);
 +extern long
+pci_physdev_op(
+    int cmd, XEN_GUEST_HANDLE_PARAM(void) arg);
+
 /*
  * To allow safe resume of do_memory_op() after preemption, we need to know
  * at what point in the page list to resume. For this purpose I steal the
Signature · 1 line
Julien Grall
#35 lore raw

Re: xen/arm: Missing appropriate locking for the IOMMU (WAS Re: [PATCH v5 02/11] xen/arm: Add PHYSDEVOP_pci_device_(*add/remove) support for ARM)

Quoted 1 line · Bertrand Marquis

Hi Julien,

Hi Bertand,

Quoted 6 lines
Quoted 6 lines · Julien Grall

Hi all,

While going through the passthrough code. I noticed that we don't have a common lock for the IOMMU between the PCI and DT code.

Quoted 7 lines
Quoted 3 lines

This is going to be an issue given it would technically be possible to add a PCI device while assigning a DT.

Rahul, Bertrand, Oleksandr, can you have a look at the issue?

Yes we can have a look at this.

Right now pci device add is done by dom0 so I do not think we have an issue in practice unless I wrongly understood something

This will depend on the XSM policy. With the default one, then yes I agree that only dom0 (we don't support hardware domain) can add PCI device.

However, this restriction doesn't really matter here. You would be relying on dom0 to do the locking and AFAIK this doesn't exist. Instead, the admin would have to ensure that two don't happen at the same time.

Anyway, I think Xen should take care of preventing concurrent IOMMU operations rather than relying on external subsystem (e.g. dom0) to do it. At least the Arm SMMU driver will rely the generic locking to modify atomically internal list.

Cheers,

Signature · 1 line
Julien Grall
#36 lore raw

Re: xen/arm: Missing appropriate locking for the IOMMU (WAS Re: [PATCH v5 02/11] xen/arm: Add PHYSDEVOP_pci_device_(*add/remove) support for ARM)

Hi Julien,

Quoted 17 lines · Julien Grall
Quoted 1 line · Bertrand Marquis

Hi Julien,

Hi Bertand,

Quoted 8 lines
Quoted 8 lines · Julien Grall

Hi all,

While going through the passthrough code. I noticed that we don't have a common lock for the IOMMU between the PCI and DT code.

This is going to be an issue given it would technically be possible to add a PCI device while assigning a DT.

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Rahul, Bertrand, Oleksandr, can you have a look at the issue?

Yes we can have a look at this.
Right now pci device add is done by dom0 so I do not think we have an issue in practice unless I wrongly understood something

This will depend on the XSM policy. With the default one, then yes I agree that only dom0 (we don't support hardware domain) can add PCI device.

However, this restriction doesn't really matter here. You would be relying on dom0 to do the locking and AFAIK this doesn't exist. Instead, the admin would have to ensure that two don't happen at the same time.

Anyway, I think Xen should take care of preventing concurrent IOMMU operations rather than relying on external subsystem (e.g. dom0) to do it. At least the Arm SMMU driver will rely the generic locking to modify atomically internal list.

Agree, was just trying to make sure I understood the problem correctly. We will check that.

Cheers
Bertrand

Quoted 5 lines

Cheers,

Signature · 1 line
Julien Grall