Skip to contentSkip to conversation

Implement PCI device enumeration on Arm

PATCH In review Mykyta Poturai 2 participants 12 messages Nov 18, 2025

lore
Share & tools
lore
#0 lore raw

v3 cover Implement PCI device enumeration on Arm

This series adds basic PCI device enumeration in Xen on Arm. This will allow us to not rely on Dom0 enumeration for supported controllers, which will enable PCI passthrough for dom0less setups.

Enumeration is disabled by default and can be enabled with "pci-scan" cmdline option.

For now the discovered devices are only assigned to HW domain. To achieve this, several things need to be done:
1. A VPCI node is created for HW domain device tree, and the real PCI nodes are hidden from it.
2. Discovered devices BARs are initialized.
3. Register handles for VPCI are updated to change behaviour depending on whether or not the calling domain uses VPCI or HW PCI, instead of relying on is_hardware_domain()

Tested on QEMU.
Arm:
* pci-scan=yes pci-passthrough=yes
* pci-scan=no pci-passthrough=yes
* pci-scan=no pci-passthrough=no
X86:
* no special options

v2->v3:
* add arm/pci: Mark pci_host_common_probe as init
* reorder patches
* see individual patches

v1->v2:
* drop xen/pci: disable pci_device_{add,remove} when hwdom uses vpci on arm
* see individual patches

Edward Pickup (1):
arm/pci: Add pci-scan boot argument

Luca Fancellu (2):
xen/pci: update DT for hwdom when it uses vpci
xen/pci: assign discovered devices to hwdom

Mykyta Poturai (2):
arm/pci: Mark pci_host_common_probe as init arm/pci: enable vpci for hwdom when pci-scan is enabled

Stefano Stabellini (1):
xen/vpci: introduce has_vpci_bridge

Stewart Hildebrand (2):
xen/dt: pass flags to callback in dt_for_each_range() xen/pci: initialize BARs

 docs/misc/xen-command-line.pandoc    |   9 ++
 xen/arch/arm/device.c                |   4 +-
 xen/arch/arm/domain_build.c          | 154 ++++++++++++++++++++++++++-
 xen/arch/arm/include/asm/domain.h    |   3 +-
 xen/arch/arm/include/asm/pci.h       |  10 ++
 xen/arch/arm/include/asm/setup.h     |   2 +-
 xen/arch/arm/pci/pci-host-common.c   | 110 ++++++++++++++++++-
 xen/arch/arm/pci/pci.c               | 139 +++++++++++++++++++++++-
 xen/common/device-tree/device-tree.c |   5 +-
 xen/common/rangeset.c                |  62 ++++++++++-
 xen/drivers/passthrough/arm/iommu.c  |  15 +++
 xen/drivers/passthrough/pci.c        |  11 ++
 xen/drivers/vpci/header.c            |  74 ++++++-------
 xen/drivers/vpci/vpci.c              |   4 +-
 xen/include/xen/device_tree.h        |   2 +-
 xen/include/xen/rangeset.h           |  11 ++
 xen/include/xen/vpci.h               |   8 ++
 17 files changed, 565 insertions(+), 58 deletions(-)
Signature · 1 line
2.51.2
#1 lore raw

PATCH v3 1/8 arm/pci: Add pci-scan boot argument

From: Edward Pickup <[email protected]>

This patch adds a Xen boot arguments that, if enabled, causes a call to existing code to scan pci devices enumerated by the firmware.

This will be needed ahead of dom0less support for pci passthrough on arm.

---

v2->v3:
* add define for cases when HAS_PCI is not set

v1->v2:
* remove dead code
* don't return errors from pci_init, but report them

changes from previous series:
* remove is_pci_scan_enabled wrapper
* make pci_scan_enabled ro_after_init
* drop debug prints
* drop Edward's SOB

---
 docs/misc/xen-command-line.pandoc  |  7 +++++++
 xen/arch/arm/include/asm/pci.h     |  2 ++
 xen/arch/arm/pci/pci-host-common.c |  1 +
 xen/arch/arm/pci/pci.c             | 29 +++++++++++++++++++++++++++--
 4 files changed, 37 insertions(+), 2 deletions(-)
docs/misc/xen-command-line.pandoc +7 -0
index 34004ce282..e5f7275bdc 100644
@@ -2079,6 +2079,13 @@ This option can be specified more than once (up to 8 times at present).
 
 Flag to enable or disable support for PCI passthrough
 
+### pci-scan (arm)
+> `= <boolean>`
+
+> Default: `false`
+
+Flag to enable or disable Xen PCI scan at boot.
+
 ### pcid (x86)
 > `= <boolean> | xpti=<bool>`
 
xen/arch/arm/include/asm/pci.h +2 -0
index 08ffcd4438..0680b2f50c 100644
@@ -155,6 +155,8 @@ bool arch_pci_device_physdevop(void);
 
 #else   /*!CONFIG_HAS_PCI*/
 
+#define pci_scan_enabled false
+
 struct pci_dev;
 
 static inline void arch_pci_init_pdev(struct pci_dev *pdev) {}
xen/arch/arm/pci/pci-host-common.c +1 -0
index 487c545f3a..d3481b05eb 100644
@@ -284,6 +284,7 @@ pci_host_common_probe(struct dt_device_node *dev,
     }
 
     pci_add_host_bridge(bridge);
+    pci_add_segment(bridge->segment);
 
     return bridge;
 
xen/arch/arm/pci/pci.c +27 -2
index beb1f971fa..49ee003c5e 100644
@@ -91,8 +91,14 @@ bool arch_pci_device_physdevop(void)
 bool __read_mostly pci_passthrough_enabled;
 boolean_param("pci-passthrough", pci_passthrough_enabled);
 
+/* By default pci scan is disabled. */
+static __ro_after_init bool pci_scan_enabled;
+boolean_param("pci-scan", pci_scan_enabled);
+
 static int __init pci_init(void)
 {
+    int ret;
+
     /*
      * Enable PCI passthrough when has been enabled explicitly
      * (pci-passthrough=on).
@@ -104,9 +110,28 @@ static int __init pci_init(void)
         panic("Could not initialize PCI segment 0\n");
 
     if ( acpi_disabled )
-        return dt_pci_init();
+        ret = dt_pci_init();
     else
-        return acpi_pci_init();
+        ret = acpi_pci_init();
+
+    if ( ret < 0 )
+    {
+        printk(XENLOG_ERR "PCI: Failed to initialize PCI host bridges (rc=%d)\n", ret);
+        return 0;
+    }
+
+    if ( pci_scan_enabled )
+    {
+        ret = scan_pci_devices();
+
+        if ( ret < 0 )
+        {
+            printk(XENLOG_ERR "PCI: Failed to scan PCI devices (rc=%d)\n", ret);
+            return 0;
+        }
+    }
+
+    return 0;
 }
 __initcall(pci_init);
 
Signature · 1 line
2.51.2
#2 lore raw

PATCH v3 2/8 xen/dt: pass flags to callback in dt_for_each_range()

In reply to Mykyta Poturai

From: Stewart Hildebrand <[email protected]>

PCI ranges have prefetchable / non-prefetchable flags that will be needed in the callback. Pass the flags to the callback.

---
v2->v3:
* style fixes

v1->v2:
* no changes

---
 xen/arch/arm/device.c                | 4 ++--
 xen/arch/arm/domain_build.c          | 3 ++-
 xen/arch/arm/include/asm/setup.h     | 2 +-
 xen/arch/arm/pci/pci-host-common.c   | 4 ++--
 xen/common/device-tree/device-tree.c | 5 +++--
 xen/include/xen/device_tree.h        | 2 +-
 6 files changed, 11 insertions(+), 9 deletions(-)
xen/arch/arm/device.c +2 -2
index 74b54cad34..732c84e726 100644
@@ -49,7 +49,7 @@ int map_irq_to_domain(struct domain *d, unsigned int irq,
     return 0;
 }
 
-int map_range_to_domain(const struct dt_device_node *dev,
+int map_range_to_domain(const struct dt_device_node *dev, uint32_t flags,
                         uint64_t addr, uint64_t len, void *data)
 {
     struct map_range_data *mr_data = data;
@@ -325,7 +325,7 @@ int handle_device(struct domain *d, struct dt_device_node *dev, p2m_type_t p2mt,
             return res;
         }
 
-        res = map_range_to_domain(dev, addr, size, &mr_data);
+        res = map_range_to_domain(dev, 0, addr, size, &mr_data);
         if ( res )
             return res;
     }
xen/arch/arm/domain_build.c +2 -1
index fb8fbb1650..4bbffdf535 100644
@@ -762,7 +762,8 @@ int __init add_ext_regions(unsigned long s_gfn, unsigned long e_gfn,
 }
 
 static int __init handle_pci_range(const struct dt_device_node *dev,
-                                   uint64_t addr, uint64_t len, void *data)
+                                   uint32_t flags, uint64_t addr, uint64_t len,
+                                   void *data)
 {
     struct rangeset *mem_holes = data;
     paddr_t start, end;
xen/arch/arm/include/asm/setup.h +1 -1
index 1eaf13bd66..97bc5f90a1 100644
@@ -62,7 +62,7 @@ int map_device_irqs_to_domain(struct domain *d, struct dt_device_node *dev,
 int map_irq_to_domain(struct domain *d, unsigned int irq,
                       bool need_mapping, const char *devname);
 
-int map_range_to_domain(const struct dt_device_node *dev,
+int map_range_to_domain(const struct dt_device_node *dev, uint32_t flags,
                         uint64_t addr, uint64_t len, void *data);
 
 struct init_info
xen/arch/arm/pci/pci-host-common.c +2 -2
index d3481b05eb..46e7e3b707 100644
@@ -418,7 +418,7 @@ int __init pci_host_bridge_mappings(struct domain *d)
                     bridge->child_ops->need_p2m_hwdom_mapping(d, bridge, addr);
             if ( need_mapping )
             {
-                err = map_range_to_domain(dev, addr, size, &mr_data);
+                err = map_range_to_domain(dev, 0, addr, size, &mr_data);
                 if ( err )
                     return err;
             }
@@ -433,7 +433,7 @@ int __init pci_host_bridge_mappings(struct domain *d)
  * to be page aligned. We should check for alignment but this is not the
  * right place for alignment check.
  */
-static int is_bar_valid(const struct dt_device_node *dev,
+static int is_bar_valid(const struct dt_device_node *dev, uint32_t flags,
                         uint64_t addr, uint64_t len, void *data)
 {
     struct pdev_bar_check *bar_data = data;
xen/common/device-tree/device-tree.c +3 -2
index 0b5375f151..5ee1fa4eb1 100644
@@ -976,7 +976,7 @@ int dt_device_get_paddr(const struct dt_device_node *dev, unsigned int index,
 
 int dt_for_each_range(const struct dt_device_node *dev,
                       int (*cb)(const struct dt_device_node *dev,
-                                uint64_t addr, uint64_t length,
+                                uint32_t flags, uint64_t addr, uint64_t length,
                                 void *data),
                       void *data)
 {
@@ -1041,13 +1041,14 @@ int dt_for_each_range(const struct dt_device_node *dev,
     {
         uint64_t a, s;
         int ret;
+        uint32_t flags = bus->get_flags(ranges);
 
         memcpy(addr, ranges + na, 4 * pna);
 
         a = __dt_translate_address(dev, addr, "ranges");
         s = dt_read_number(ranges + na + pna, ns);
 
-        ret = cb(dev, a, s, data);
+        ret = cb(dev, flags, a, s, data);
         if ( ret )
         {
             dt_dprintk(" -> callback failed=%d\n", ret);
xen/include/xen/device_tree.h +1 -1
index 06d7643622..1091e34a10 100644
@@ -667,7 +667,7 @@ int dt_for_each_irq_map(const struct dt_device_node *dev,
  */
 int dt_for_each_range(const struct dt_device_node *dev,
                       int (*cb)(const struct dt_device_node *dev,
-                                uint64_t addr, uint64_t length,
+                                uint32_t flags, uint64_t addr, uint64_t length,
                                 void *data),
                       void *data);
 
Signature · 1 line
2.51.2
#3 lore raw

PATCH v3 3/8 xen/vpci: introduce has_vpci_bridge

In reply to Mykyta Poturai

From: Stefano Stabellini <[email protected]>

With Dom0 now being able to use a virtual bridge we need a way to distinguish Dom0 using HW bridghe from Dom0 using virtual bridge. Introduce a new macro has_vpci_bridge that would determine if a domain should be treated as using HW bridge (only Dom0 with pci-scan disabled) or as using a virtual one (all DomUs and Dom0 with pci-scan enabled)

Use the macro in drivers/vpci.

---
v2->v3:
* s/is_hwdom/is_hw_bridge/

v1->v2:
* simplify definition

---
 xen/arch/arm/include/asm/pci.h |  1 +
 xen/arch/arm/pci/pci.c         |  2 +-
 xen/drivers/vpci/header.c      | 74 +++++++++++++++++-----------------
 xen/drivers/vpci/vpci.c        |  4 +-
 xen/include/xen/vpci.h         |  8 ++++
 5 files changed, 50 insertions(+), 39 deletions(-)
xen/arch/arm/include/asm/pci.h +1 -0
index 0680b2f50c..7289f7688b 100644
@@ -23,6 +23,7 @@
 #define pci_to_dev(pcidev) (&(pcidev)->arch.dev)
 
 extern bool pci_passthrough_enabled;
+extern bool pci_scan_enabled;
 
 struct rangeset;
 
xen/arch/arm/pci/pci.c +1 -1
index 49ee003c5e..951639eb3f 100644
@@ -92,7 +92,7 @@ bool __read_mostly pci_passthrough_enabled;
 boolean_param("pci-passthrough", pci_passthrough_enabled);
 
 /* By default pci scan is disabled. */
-static __ro_after_init bool pci_scan_enabled;
+__ro_after_init bool pci_scan_enabled;
 boolean_param("pci-scan", pci_scan_enabled);
 
 static int __init pci_init(void)
xen/drivers/vpci/header.c +38 -36

209 lines not on this page · raw message

xen/drivers/vpci/vpci.c +2 -2
index 07c7071d0a..8ea89b9805 100644
@@ -48,7 +48,7 @@ static int assign_virtual_sbdf(struct pci_dev *pdev)
 
     ASSERT(rw_is_write_locked(&pdev->domain->pci_lock));
 
-    if ( is_hardware_domain(d) )
+    if ( !has_vpci_bridge(d) )
         return 0;
 
     /*
@@ -429,7 +429,7 @@ static const struct pci_dev *translate_virtual_device(const struct domain *d,
 #ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT
     const struct pci_dev *pdev;
 
-    ASSERT(!is_hardware_domain(d));
+    ASSERT(has_vpci_bridge(d));
     ASSERT(rw_is_locked(&d->pci_lock));
 
     for_each_pdev ( d, pdev )
xen/include/xen/vpci.h +8 -0
index 9ae75d946a..d4695cb353 100644
@@ -339,6 +339,14 @@ static inline int __must_check vpci_reset_device(struct pci_dev *pdev)
     return vpci_assign_device(pdev);
 }
 
+#ifdef CONFIG_ARM
+#include <asm/pci.h>
+
+#define has_vpci_bridge(d) (!is_hardware_domain(d) || pci_scan_enabled)
+#else
+#define has_vpci_bridge(d) (!is_hardware_domain(d))
+#endif
+
 #endif
 
 /*
Signature · 1 line
2.51.2
#4 lore raw

Re: [PATCH v3 3/8] xen/vpci: introduce has_vpci_bridge

Quoted 7 lines · Mykyta Poturai

From: Stefano Stabellini <[email protected]>

With Dom0 now being able to use a virtual bridge we need a way to distinguish Dom0 using HW bridghe from Dom0 using virtual bridge. Introduce a new macro has_vpci_bridge that would determine if a domain should be treated as using HW bridge (only Dom0 with pci-scan disabled) or as using a virtual one (all DomUs and Dom0 with pci-scan enabled)

Where does the connection of "pci-scan {en,dis}abled" with "kind of bridges in use" come from? There is a connection for what you're trying to achieve right now, but this doesn't want setting in stone now, to avoid making it harder to decouple the two again later.

xen/drivers/vpci/header.c bool vpci_process_pending(struct vcpu *v) · line 234

Quoted 9 lines
xen/drivers/vpci/header.c +1 -1
@@ -230,7 +230,7 @@ bool vpci_process_pending(struct vcpu *v)
 
             read_unlock(&v->domain->pci_lock);
 
-            if ( !is_hardware_domain(v->domain) )
+            if ( has_vpci_bridge(v->domain) )
                 domain_crash(v->domain);

At this example (applies more or less similarly elsewhere as well, and needs answering separately for every instance), and effectively re-iterating a point made previously: Why is it the kind of bridges that are used which determines whether to call domain_crash() here?

Jan

#5 lore raw

PATCH v3 4/8 xen/pci: update DT for hwdom when it uses vpci

In reply to Mykyta Poturai

From: Luca Fancellu <[email protected]>

When pci-scan is enabled and Xen supports vpci for guests, Xen will scan the pci bus to find devices and emulate the pci bus, so the hw domain must see the emulated bus instead of the real one.

A new helper function, hwdom_uses_vpci, is implemented and returns true when pci-scan is enabled and Xen is built with CONFIG_HAS_VPCI_GUEST_SUPPORT=y. When hwdom_uses_vpci() is true, a vpci node is created for the hwdom device tree.

Depending on whether the guest is using vPCI or not, and whether the domain is using host layout or not, generate the appropriate device tree nodes for the guest and handle the right MMIO regions traps.

---
v2->v3:
* use has_vpci_bridge in place of hwdom_uses_vpci

v1->v2:
* make make_vpci_node static

---
 docs/misc/xen-command-line.pandoc |   4 +-
 xen/arch/arm/domain_build.c       | 151 +++++++++++++++++++++++++++++-
 2 files changed, 153 insertions(+), 2 deletions(-)
docs/misc/xen-command-line.pandoc +3 -1
index e5f7275bdc..4a546f94a7 100644
@@ -2084,7 +2084,9 @@ Flag to enable or disable support for PCI passthrough
 
 > Default: `false`
 
-Flag to enable or disable Xen PCI scan at boot.
+Flag to enable or disable Xen PCI scan at boot. When the flag is enabled, the
+hardware domain cannot have access to the real PCI bus, it will see the bus
+emulated by Xen.
 
 ### pcid (x86)
 > `= <boolean> | xpti=<bool>`
xen/arch/arm/domain_build.c +150 -1

175 lines not on this page · raw message

Signature · 1 line
2.51.2
#6 lore raw

PATCH v3 5/8 arm/pci: Mark pci_host_common_probe as init

In reply to Mykyta Poturai

pci_host_common_probe is only ever called from init context so mark it as such.

---
v2->v3:
* patch introduced

---
 xen/arch/arm/pci/pci-host-common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/arm/pci/pci-host-common.c +1 -1
index 46e7e3b707..de30fb0aec 100644
@@ -232,7 +232,7 @@ static int pci_bus_find_domain_nr(struct dt_device_node *dev)
     return domain;
 }
 
-struct pci_host_bridge *
+struct pci_host_bridge * __init
 pci_host_common_probe(struct dt_device_node *dev,
                       const struct pci_ecam_ops *ops,
                       const struct pci_ecam_ops *child_ops)
Signature · 1 line
2.51.2
#7 lore raw

PATCH v3 6/8 xen/pci: initialize BARs

In reply to Mykyta Poturai

From: Stewart Hildebrand <[email protected]>

A PCI device must have valid BARs in order to assign it to a domain. On ARM, firmware is unlikely to have initialized the BARs, so we must do this in Xen. During setup_hwdom_pci_devices(), check if each BAR is valid. If the BAR happens to already be valid, remove the BAR range from a rangeset of valid PCI ranges so as to avoid overlap when reserving a new BAR. If not valid, reserve a new BAR address from the rangeset and write it to the device.

Avaliable ranges are read from DT during init and stored in two distinct rangesets, one for prefetchable and one for non-prefetchable BARs.

---
v2->v3:
* drop hwdom_uses_vpci
* check that rangeset can handle u64
* rework rangeset manipulaiton
* mark more functions as __init
* move bar init to arm files
* style fixes

v1->v2:
* move hwdom_uses_vpci to this patch
* fixup error reporting

---
 xen/arch/arm/include/asm/pci.h     |   7 ++
 xen/arch/arm/pci/pci-host-common.c |  92 ++++++++++++++++++++++++
 xen/arch/arm/pci/pci.c             | 110 +++++++++++++++++++++++++++++
 xen/common/rangeset.c              |  62 ++++++++++++++--
 xen/include/xen/rangeset.h         |  11 +++
 5 files changed, 277 insertions(+), 5 deletions(-)
xen/arch/arm/include/asm/pci.h +7 -0
index 7289f7688b..ac4e87f9c1 100644
@@ -74,6 +74,8 @@ struct pci_host_bridge {
     struct pci_config_window *child_cfg;
     const struct pci_ops *child_ops;
     void *priv;                      /* Private data of the bridge. */
+    struct rangeset *bar_ranges;
+    struct rangeset *bar_ranges_prefetch;
 };
 
 struct pci_ops {
@@ -154,6 +156,11 @@ void pci_generic_init_bus_range_child(struct dt_device_node *dev,
 
 bool arch_pci_device_physdevop(void);
 
+uint64_t pci_get_new_bar_addr(const struct pci_dev *pdev, uint64_t size,
+                              bool is_64bit, bool prefetch);
+int pci_reserve_bar_range(const struct pci_dev *pdev, uint64_t addr,
+                          uint64_t size, bool prefetch);
+
 #else   /*!CONFIG_HAS_PCI*/
 
 #define pci_scan_enabled false
xen/arch/arm/pci/pci-host-common.c +92 -0

116 lines not on this page · raw message

xen/arch/arm/pci/pci.c +110 -0

122 lines not on this page · raw message

xen/common/rangeset.c +57 -5

87 lines not on this page · raw message

xen/include/xen/rangeset.h +11 -0
index 817505badf..dcef96cb2c 100644
@@ -61,6 +61,17 @@ int __must_check rangeset_add_range(
     struct rangeset *r, unsigned long s, unsigned long e);
 int __must_check rangeset_claim_range(struct rangeset *r, unsigned long size,
                                       unsigned long *s);
+
+/*
+ * Find a range subset that starts at or after s, ends before e,
+ * and is aligned to the size.
+ * If such subset is present it is removed from the rangeset and
+ * it's start is written to s, otherwise s is set to 0.
+ */
+int __must_check rangeset_claim_aligned_range(struct rangeset *r,
+                                              unsigned long size,
+                                              unsigned long *s,
+                                              unsigned long e);
 int __must_check rangeset_remove_range(
     struct rangeset *r, unsigned long s, unsigned long e);
 bool __must_check rangeset_contains_range(
Signature · 1 line
2.51.2
#8 lore raw

Re: [PATCH v3 6/8] xen/pci: initialize BARs

xen/arch/arm/pci/pci-host-common.c static int pci_bus_find_domain_nr(struct dt_device_node *dev) · line 243

Quoted 12 lines · Mykyta Poturai
xen/arch/arm/pci/pci-host-common.c +8 -0
@@ -232,6 +233,25 @@ static int pci_bus_find_domain_nr(struct dt_device_node *dev)
     return domain;
 }
 
+static int __init add_bar_range(const struct dt_device_node *dev,
+                                uint32_t flags, uint64_t addr, uint64_t len,
+                                void *data)
+{
+    struct pci_host_bridge *bridge = data;
+
+    /* Ensure we are not using bits in a rangeset */
+    BUILD_BUG_ON(sizeof(unsigned long) != sizeof(uint64_t));

Can you please help me interpret the comment?

Also, rather than != isn't < sufficient to check for?

Quoted 5 lines
xen/arch/arm/pci/pci-host-common.c +1 -0
@@ -283,6 +303,18 @@ pci_host_common_probe(struct dt_device_node *dev,
         bridge->child_ops = &child_ops->pci_ops;
     }
 
+    bridge->bar_ranges = rangeset_new(NULL, "BAR ranges",

xen/arch/arm/pci/pci-host-common.c pci_host_common_probe(struct dt_device_node *dev, · line 316

Quoted 10 lines
xen/arch/arm/pci/pci-host-common.c +10 -0
+                                      RANGESETF_prettyprint_hex);
+    bridge->bar_ranges_prefetch = rangeset_new(NULL,
+                                               "BAR ranges (prefetchable)",
+                                               RANGESETF_prettyprint_hex);
+    if ( bridge->bar_ranges && bridge->bar_ranges_prefetch )
+    {
+        err = dt_for_each_range(bridge->dt_node, add_bar_range, bridge);
+        if ( err )
+            goto err_child;
+    }

I'm pretty sure I commented on this already: Without an "else" use sites of the two rangesets need to have NULL checks added, plus imo there would want to be a comment here explaining to readers why omitting the "else" (and hence proper error handling) is okay.

xen/arch/arm/pci/pci-host-common.c bool pci_check_bar(const struct pci_dev *pdev, mfn_t start, mfn_t end) · line 517

Quoted 11 lines
xen/arch/arm/pci/pci-host-common.c +7 -0
@@ -476,6 +508,66 @@ bool pci_check_bar(const struct pci_dev *pdev, mfn_t start, mfn_t end)
 
     return bar_data.is_valid;
 }
+
+/*
+ * Find suitable place for an uninitialized bar of specified size in the
+ * host bridge ranges
+ */
+uint64_t __init pci_get_new_bar_addr(const struct pci_dev *pdev, uint64_t size,
+                                     bool is_64bit, bool prefetch)

Seeing the comment - why only "host bridge"? Especially for Dom0, if other bridges are present in the system, I think you won't get away without having a virtual counterpart for evey one of them (or alternatively without hiding all of them plus the devices behind them).

xen/arch/arm/pci/pci-host-common.c bool pci_check_bar(const struct pci_dev *pdev, mfn_t start, mfn_t end) · line 524

Quoted 7 lines
xen/arch/arm/pci/pci-host-common.c +7 -0
+{
+    struct pci_host_bridge *bridge;
+    struct rangeset *range;
+    uint64_t addr = 0, end = GB(4);
+
+    /* Make sure we can store addr in a rangeset */
+    BUILD_BUG_ON(sizeof(addr) != sizeof(unsigned long));

While "store" looks right here, ...

Quoted 25 lines
xen/arch/arm/pci/pci-host-common.c +25 -0
+    bridge = pci_find_host_bridge(pdev->seg, pdev->bus);
+    if ( !bridge )
+        return 0;
+
+    range = prefetch ? bridge->bar_ranges_prefetch : bridge->bar_ranges;
+
+    if ( size < PAGE_SIZE )
+        size = PAGE_SIZE;
+
+    if ( is_64bit )
+    {
+        addr = GB(4);
+        end = ~0;
+    }
+
+    if ( !rangeset_claim_aligned_range(range, size, &addr, end) )
+        return addr;
+
+    printk(XENLOG_ERR "Failed to claim BAR range %lx-%lx from rangeset\n",
+           addr, addr + size - 1);
+
+    return 0;
+}
+
+/*

xen/arch/arm/pci/pci-host-common.c bool pci_check_bar(const struct pci_dev *pdev, mfn_t start, mfn_t end) · line 560

Quoted 10 lines
xen/arch/arm/pci/pci-host-common.c +10 -0
+ * Remove already used memory from the host bridge bar ranges
+ */
+int __init pci_reserve_bar_range(const struct pci_dev *pdev, uint64_t addr,
+                                 uint64_t size, bool prefetch)
+{
+    struct pci_host_bridge *bridge;
+    struct rangeset *range;
+
+    /* Make sure we can store addr in a rangeset */
+    BUILD_BUG_ON(sizeof(addr) != sizeof(unsigned long));

... it doen't here, as ...

xen/arch/arm/pci/pci-host-common.c bool pci_check_bar(const struct pci_dev *pdev, mfn_t start, mfn_t end) · line 568

Quoted 7 lines
xen/arch/arm/pci/pci-host-common.c +7 -0
+    bridge = pci_find_host_bridge(pdev->seg, pdev->bus);
+    if ( !bridge )
+        return 0;
+
+    range = prefetch ? bridge->bar_ranges_prefetch : bridge->bar_ranges;
+
+    return rangeset_remove_range(range, addr, addr + size - 1);

... there's nothing being stored.

But I'm apparently confused in a broader way: Here you remove a range from the selected rangeset. rangeset_claim_aligned_range() also does so. Why are there two removals?

xen/arch/arm/pci/pci.c boolean_param("pci-passthrough", pci_passthrough_enabled); · line 99

Quoted 8 lines
xen/arch/arm/pci/pci.c +2 -0
@@ -95,6 +95,108 @@ boolean_param("pci-passthrough", pci_passthrough_enabled);
 __ro_after_init bool pci_scan_enabled;
 boolean_param("pci-scan", pci_scan_enabled);
 
+typedef int (*bar_callback_t)(struct pci_dev *, uint8_t, uint64_t, uint64_t,
+                              bool, bool);

Hmm, okay, you have a typedef now. But ...

xen/arch/arm/pci/pci.c boolean_param("pci-passthrough", pci_passthrough_enabled); · line 103

Quoted 3 lines
xen/arch/arm/pci/pci.c +3 -0
+static int __init reserve_bar_range(struct pci_dev *pdev, uint8_t reg,
+                                    uint64_t addr, uint64_t size, bool is_64bit,
+                                    bool prefetch)

... if I altered e.g. this function's signature, ...

Quoted 26 lines
xen/arch/arm/pci/pci.c +26 -0
+{
+    if ( pci_check_bar(pdev, maddr_to_mfn(addr),
+                       maddr_to_mfn(addr + size - 1)) )
+        return pci_reserve_bar_range(pdev, addr, size, prefetch);
+    return 0;
+}
+
+static int __init setup_bar(struct pci_dev *pdev, uint8_t reg, uint64_t addr,
+                            uint64_t size, bool is_64bit, bool prefetch)
+{
+    if ( !pci_check_bar(pdev, maddr_to_mfn(addr),
+                        maddr_to_mfn(addr + size - 1)) )
+    {
+        uint16_t cmd = pci_conf_read16(pdev->sbdf, PCI_COMMAND);
+
+        addr = pci_get_new_bar_addr(pdev, size, is_64bit, prefetch);
+        if ( !addr )
+            return -ENOMEM;
+
+        pci_conf_write16(pdev->sbdf, PCI_COMMAND,
+                         cmd & ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO));
+
+        pci_conf_write32(pdev->sbdf, reg,
+                         (addr & GENMASK(31, 0)) |
+                         (is_64bit ? PCI_BASE_ADDRESS_MEM_TYPE_64 : 0));
+

xen/arch/arm/pci/pci.c boolean_param("pci-passthrough", pci_passthrough_enabled); · line 139

Quoted 10 lines
xen/arch/arm/pci/pci.c +10 -0
+        if ( is_64bit )
+            pci_conf_write32(pdev->sbdf, reg + 4, addr >> 32);
+
+        pci_conf_write16(pdev->sbdf, PCI_COMMAND, cmd);
+    }
+
+    return 0;
+}
+
+static int __init bars_iterate(struct pci_dev *pdev, void *arg)

... the use of void * here still renders things type-unsafe. Plus you needlessly introduce function pointer <=> data pointer conversions, which Misra wants us to avoid. IOW ...

xen/arch/arm/pci/pci.c boolean_param("pci-passthrough", pci_passthrough_enabled); · line 143

Quoted 4 lines
xen/arch/arm/pci/pci.c +4 -0
+{
+    unsigned int i, barsize, ret = 0, num_bars = PCI_HEADER_NORMAL_NR_BARS;
+    uint64_t addr, size;
+    bar_callback_t cb = arg;

... this (bar_callback_t cb) is what the function parameter wants to be. Ideally though as "bar_callback_t *cb" to not hide the pointer-ness, then requiring the pointer part to be dropped from the typedef itself.

Quoted 32 lines
xen/arch/arm/pci/pci.c +32 -0
+    if ( (pci_conf_read8(pdev->sbdf, PCI_HEADER_TYPE) & 0x7f) ==
+         PCI_HEADER_TYPE_NORMAL )
+    {
+        for ( i = 0; i < num_bars; i += barsize )
+        {
+            uint8_t reg = PCI_BASE_ADDRESS_0 + i * 4;
+            bool prefetch;
+
+            if ( (pci_conf_read32(pdev->sbdf, reg) & PCI_BASE_ADDRESS_SPACE) ==
+                 PCI_BASE_ADDRESS_SPACE_IO )
+            {
+                barsize = 1;
+                continue;
+            }
+
+            barsize = pci_size_mem_bar(pdev->sbdf, reg, &addr, &size,
+                                       (i == num_bars - 1) ? PCI_BAR_LAST : 0);
+
+            if ( !size )
+                continue;
+
+            prefetch = pci_conf_read32(pdev->sbdf, reg) &
+                       PCI_BASE_ADDRESS_MEM_PREFETCH;
+
+            ret = cb(pdev, reg, addr, size, barsize == 2, prefetch);
+            if ( ret )
+                return ret;
+        }
+    }
+
+    return ret;
+}

xen/arch/arm/pci/pci.c boolean_param("pci-passthrough", pci_passthrough_enabled); · line 186

Quoted 10 lines
xen/arch/arm/pci/pci.c +10 -0
+
+static int __init pci_setup_bars(void)
+{
+    int ret;
+    /* We can't change the signature of bars_iterate to only accept
+     * bar_callback_t, so use intermediate variables to ensure callback
+     * signatures are always correct
+     */
+    bar_callback_t cb_reserve = reserve_bar_range;
+    bar_callback_t cb_setup = setup_bar;

Oh, here you actually fake partial type-safety. This should be dropped.

xen/arch/arm/pci/pci.c int rangeset_remove_range( · line 244

Quoted 6 lines
xen/arch/arm/pci/pci.c +1 -1
@@ -244,8 +241,18 @@ int rangeset_remove_range(
             destroy_range(r, x);
     }
 
- out:
+out:

Why are you violating style here? See ./CODING_STYLE, also for other labels you introduce.

xen/arch/arm/pci/pci.c int rangeset_remove_range( · line 250

Quoted 6 lines
xen/arch/arm/pci/pci.c +6 -0
+    return rc;
+}
+
+int rangeset_remove_range(struct rangeset *r, unsigned long s, unsigned long e)
+{
+    int rc = 0;

Pointless initializer.

Quoted 8 lines
xen/arch/arm/pci/pci.c +3 -0
+    write_lock(&r->lock);
+    rc = remove_range(r, s, e);
     write_unlock(&r->lock);
+
     return rc;
 }
 

xen/arch/arm/pci/pci.c int rangeset_claim_range(struct rangeset *r, unsigned long size, · line 371

Quoted 8 lines
xen/arch/arm/pci/pci.c +5 -0
@@ -357,6 +364,51 @@ int rangeset_claim_range(struct rangeset *r, unsigned long size,
     return 0;
 }
 
+int rangeset_claim_aligned_range(struct rangeset *r, unsigned long size,
+                                 unsigned long *s, unsigned long e)
+{
+    struct range *x;
+    int rc = 0;

Again, plus this variable looks to be used only ...

Quoted 6 lines
xen/arch/arm/pci/pci.c +6 -0
+    /* Power of 2 check */
+    if ( (size & (size - 1)) != 0 && size != 0 )
+    {
+        *s = 0;
+        return -EINVAL;
+    }

xen/arch/arm/pci/pci.c int rangeset_claim_range(struct rangeset *r, unsigned long size, · line 387

Quoted 10 lines
xen/arch/arm/pci/pci.c +10 -0
+
+    if ( e < *s )
+        return -EINVAL;
+
+    write_lock(&r->lock);
+
+    for ( x = first_range(r); x; x = next_range(r, x) )
+    {
+        /* Assumes size is a power of 2 */
+        unsigned long start_aligned = ROUNDUP(x->s, size);

I don't think the comment is very useful - you do the necessary check above, and what is said is an inherent property of ROUNDUP().

xen/arch/arm/pci/pci.c int rangeset_claim_range(struct rangeset *r, unsigned long size, · line 389

Quoted 2 lines
xen/arch/arm/pci/pci.c +2 -0
+        if ( x->e > start_aligned &&
+             (x->e - start_aligned) >= size &&

Remember that x->e is an inclusive upper bound.

xen/arch/arm/pci/pci.c int rangeset_claim_range(struct rangeset *r, unsigned long size, · line 393

Quoted 4 lines
xen/arch/arm/pci/pci.c +4 -0
+             start_aligned >= *s &&
+             start_aligned + size <= e)
+        {
+            rc = remove_range(r, start_aligned, start_aligned + size - 1);

... in the narrow scope (so should move here).

xen/arch/arm/pci/pci.c int rangeset_claim_range(struct rangeset *r, unsigned long size, · line 397

Quoted 4 lines
xen/arch/arm/pci/pci.c +4 -0
+            if ( !rc )
+                *s = start_aligned;
+            else
+                *s = 0;

Is it reasonably possible to take this path? If not, please add ASSERT_UNREACHABLE().

xen/arch/arm/pci/pci.c int rangeset_claim_range(struct rangeset *r, unsigned long size, · line 398

Quoted 1 line
xen/arch/arm/pci/pci.c +1 -0
+            write_unlock(&r->lock);

This can move up some, can't it? We want to keep locked regions as narrow as possible.

xen/arch/arm/pci/pci.c int rangeset_claim_range(struct rangeset *r, unsigned long size, · line 406

Quoted 8 lines
xen/arch/arm/pci/pci.c +8 -0
+            return rc;
+        }
+    }
+
+    *s = 0;
+
+    write_unlock(&r->lock);
+    return -ENOSPC;

Blank line please ahead of the main / final "return" of a function.

xen/include/xen/rangeset.h int __must_check rangeset_add_range( · line 67

Quoted 10 lines
xen/include/xen/rangeset.h +4 -0
@@ -61,6 +61,17 @@ int __must_check rangeset_add_range(
     struct rangeset *r, unsigned long s, unsigned long e);
 int __must_check rangeset_claim_range(struct rangeset *r, unsigned long size,
                                       unsigned long *s);
+
+/*
+ * Find a range subset that starts at or after s, ends before e,
+ * and is aligned to the size.

Is "before" correct? Isn't it "at or before", just like for s it's "at or after"?

As to "aligned", nothing contrary being said here I think I ought to be able to pass e.g. 7. (Tying together size and alignment is suitable for the BAR handling purpose you have, but is making this new interface pretty much not general- purpose.)

Jan

#9 lore raw

PATCH v3 7/8 xen/pci: assign discovered devices to hwdom

In reply to Mykyta Poturai

From: Luca Fancellu <[email protected]>

Hook up existing PCI setup routines for hwdom into Arm iommu initialization sequence, only assign endpoint devices.

During scanned PCI device assignment, also permit access to the BAR ranges if hwdom is using vpci and hide host bridges from domains that use the fully emulated one.

---
v2->v3:
* style fixes
* rework iomem access configuration

v1->v2:
* add host bridge hiding
* fix build without CONFIG_HAS_PCI

---
 xen/arch/arm/pci/pci-host-common.c  | 11 +++++++++++
 xen/drivers/passthrough/arm/iommu.c | 15 +++++++++++++++
 xen/drivers/passthrough/pci.c       | 11 +++++++++++
 3 files changed, 37 insertions(+)
xen/arch/arm/pci/pci-host-common.c +11 -0
index 28c26af9eb..110c334f59 100644
@@ -430,6 +430,17 @@ int __init pci_host_bridge_mappings(struct domain *d)
         unsigned int i;
         bool need_mapping;
 
+        /*
+         * Only allow iomem access to ranges if we use pci-scan
+         * Actual mappings will be handled by VPCI code.
+         */
+        if ( has_vpci_bridge(d) )
+        {
+            mr_data.skip_mapping = true;
+            dt_for_each_range(dev, map_range_to_domain, &mr_data);
+            continue;
+        }
+
         for ( i = 0; i < dt_number_of_address(dev); i++ )
         {
             paddr_t addr, size;
xen/drivers/passthrough/arm/iommu.c +15 -0
index 100545e23f..124a99f198 100644
@@ -19,6 +19,7 @@
 #include <xen/device_tree.h>
 #include <xen/iommu.h>
 #include <xen/lib.h>
+#include <xen/sched.h>
 
 #include <asm/device.h>
 
@@ -133,6 +134,16 @@ void arch_iommu_domain_destroy(struct domain *d)
 {
 }
 
+#ifdef CONFIG_HAS_PCI
+static int __hwdom_init iommu_add_hwdom_pci_device(u8 devfn,
+                                                   struct pci_dev *pdev)
+{
+    const struct domain_iommu *hd = dom_iommu(hardware_domain);
+
+    return iommu_call(hd->platform_ops, add_device, devfn, pci_to_dev(pdev));
+}
+#endif
+
 void __hwdom_init arch_iommu_hwdom_init(struct domain *d)
 {
     /* Set to false options not supported on ARM. */
@@ -142,6 +153,10 @@ void __hwdom_init arch_iommu_hwdom_init(struct domain *d)
     if ( iommu_hwdom_reserved == 1 )
         printk(XENLOG_WARNING "map-reserved dom0-iommu option is not supported on ARM\n");
     iommu_hwdom_reserved = 0;
+
+#ifdef CONFIG_HAS_PCI
+    setup_hwdom_pci_devices(d, iommu_add_hwdom_pci_device);
+#endif
 }
 
 /*
xen/drivers/passthrough/pci.c +11 -0
index 52c22fa50c..70c21403d5 100644
@@ -20,6 +20,7 @@
 #include <xen/pci_ids.h>
 #include <xen/list.h>
 #include <xen/prefetch.h>
+#include <xen/iocap.h>
 #include <xen/iommu.h>
 #include <xen/irq.h>
 #include <xen/param.h>
@@ -1041,6 +1042,12 @@ enum pdev_type pdev_type(u16 seg, u8 bus, u8 devfn)
     return pos ? DEV_TYPE_PCIe_ENDPOINT : DEV_TYPE_PCI;
 }
 
+static bool __hwdom_init pdev_is_endpoint(const struct pci_dev *pdev)
+{
+    enum pdev_type type = pdev_type(pdev->seg, pdev->bus, pdev->devfn);
+    return type == DEV_TYPE_PCIe_ENDPOINT || type == DEV_TYPE_PCI;
+}
+
 /*
  * find the upstream PCIe-to-PCI/PCIX bridge or PCI legacy bridge
  * return 0: the device is integrated PCI device or PCIe
@@ -1221,6 +1228,10 @@ static int __hwdom_init cf_check _setup_hwdom_pci_devices(
             if ( !pdev )
                 continue;
 
+            /* Hide real bridges from HWdom when it's using the emulated one */
+            if ( has_vpci_bridge(hardware_domain) && !pdev_is_endpoint(pdev) )
+                pci_hide_device(pdev->seg, pdev->bus, pdev->devfn);
+
             if ( !pdev->domain )
             {
                 pdev->domain = ctxt->d;
Signature · 1 line
2.51.2
#10 lore raw

Re: [PATCH v3 7/8] xen/pci: assign discovered devices to hwdom

xen/drivers/passthrough/pci.c enum pdev_type pdev_type(u16 seg, u8 bus, u8 devfn) · line 1048

Quoted 8 lines · Mykyta Poturai
xen/drivers/passthrough/pci.c +4 -0
@@ -1041,6 +1042,12 @@ enum pdev_type pdev_type(u16 seg, u8 bus, u8 devfn)
     return pos ? DEV_TYPE_PCIe_ENDPOINT : DEV_TYPE_PCI;
 }
 
+static bool __hwdom_init pdev_is_endpoint(const struct pci_dev *pdev)
+{
+    enum pdev_type type = pdev_type(pdev->seg, pdev->bus, pdev->devfn);
+    return type == DEV_TYPE_PCIe_ENDPOINT || type == DEV_TYPE_PCI;

Once again - blank line please before the main return of a function (or, for that matter, between declaration(s) and statement(s)).

xen/drivers/passthrough/pci.c static int __hwdom_init cf_check _setup_hwdom_pci_devices( · line 1233

Quoted 7 lines
xen/drivers/passthrough/pci.c +3 -0
@@ -1221,6 +1228,10 @@ static int __hwdom_init cf_check _setup_hwdom_pci_devices(
             if ( !pdev )
                 continue;
 
+            /* Hide real bridges from HWdom when it's using the emulated one */
+            if ( has_vpci_bridge(hardware_domain) && !pdev_is_endpoint(pdev) )
+                pci_hide_device(pdev->seg, pdev->bus, pdev->devfn);

As said elsewhere, when you hide bridges without introducing virtual counterparts, everything behind the bridge also needs hiding.

Jan

#11 lore raw

PATCH v3 8/8 arm/pci: enable vpci for hwdom when pci-scan is enabled

In reply to Mykyta Poturai

With pci-scan implemented it is now possible to use vpci for hardware domains. Update has_vpci to reflect this change.

---
v2->v3:
* replace hwdom_uses_vpci with has_vpci_bridge

v1->v2:
* fix typo in commit message

---
 xen/arch/arm/include/asm/domain.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
xen/arch/arm/include/asm/domain.h +1 -2
index af3e168374..3eac0aea64 100644
@@ -305,8 +305,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; })
+#define has_vpci(d)    (is_hardware_domain(d) && has_vpci_bridge(d))
 
 struct arch_vcpu_io {
     struct instr_details dabt_instr; /* when the instruction is decoded */
Signature · 1 line
2.51.2