Skip to contentSkip to conversation

Implement PCI device enumeration on Arm part 1

PATCH Superseded Mykyta Poturai 3 participants 18 messages Sep 24, 2025 – Nov 13, 2025

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Implement PCI device enumeration on Arm part 1

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()
4. PCI physdev ops are disabled for HW domain using VPCI.

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

Luca Fancellu (3):
xen/pci: update DT for hwdom when it uses vpci
xen/pci: disable pci_device_{add,remove} when hwdom uses vpci on arm xen/pci: assign discovered devices to hwdom

Mykyta Poturai (1):
arm/pci: enable vpci for hwdom when pci-scan is enabled

Stefano Stabellini (1):
xen/pci: 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/domain_build.h |   1 +
 xen/arch/arm/include/asm/pci.h          |  25 ++++
 xen/arch/arm/include/asm/setup.h        |   2 +-
 xen/arch/arm/pci/pci-host-common.c      |  83 ++++++++++++-
 xen/arch/arm/pci/pci.c                  |  24 +++-
 xen/arch/x86/include/asm/pci.h          |  20 +++
 xen/common/device-tree/device-tree.c    |   5 +-
 xen/common/rangeset.c                   |  35 ++++++
 xen/drivers/passthrough/arm/iommu.c     |   9 ++
 xen/drivers/passthrough/pci.c           | 119 +++++++++++++++++-
 xen/drivers/pci/physdev.c               |   6 +
 xen/drivers/vpci/header.c               |  14 +--
 xen/drivers/vpci/vpci.c                 |   4 +-
 xen/include/xen/device_tree.h           |   2 +-
 xen/include/xen/rangeset.h              |   6 +-
 xen/include/xen/vpci.h                  |   9 ++
 20 files changed, 509 insertions(+), 25 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 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.

---
(cherry picked from commit bce463e1588a45e1bfdf59fc0d5f88b16604e439 from
the downstream branch poc/pci-passthrough from
https://gitlab.com/xen-project/people/bmarquis/xen-arm-poc.git)

v1->v2:
* remove is_pci_scan_enabled wrapper
* make pci_scan_enabled ro_after_init
* drop debug prints
* drop Edward's SOB

changes since cherry-pick:
* s/always_inline/inline/
* replace additional kconfig option with config DEBUG

---
 docs/misc/xen-command-line.pandoc  |  7 +++++++
 xen/arch/arm/include/asm/pci.h     |  3 +++
 xen/arch/arm/pci/pci-host-common.c |  1 +
 xen/arch/arm/pci/pci.c             | 24 ++++++++++++++++++++++--
 4 files changed, 33 insertions(+), 2 deletions(-)
docs/misc/xen-command-line.pandoc +7 -0
index 518e42d965..4a66c5a8f9 100644
@@ -2072,6 +2072,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 +3 -0
index 08ffcd4438..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;
 
@@ -155,6 +156,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 +22 -2
index beb1f971fa..1b34e17517 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. */
+bool __ro_after_init 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,23 @@ 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 )
+        return ret;
+
+    if ( pci_scan_enabled )
+    {
+        ret = scan_pci_devices();
+
+        if ( ret < 0 )
+            return ret;
+
+    }
+
+    return 0;
 }
 __initcall(pci_init);
 
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v1 1/8] arm/pci: Add pci-scan boot argument

xen/arch/arm/include/asm/pci.h line 26

Quoted 7 lines · Mykyta Poturai
xen/arch/arm/include/asm/pci.h +1 -0
@@ -23,6 +23,7 @@
 #define pci_to_dev(pcidev) (&(pcidev)->arch.dev)
 
 extern bool pci_passthrough_enabled;
+extern bool pci_scan_enabled;

At this point of the series this isn't needed, ...

xen/arch/arm/include/asm/pci.h bool arch_pci_device_physdevop(void); · line 159

Quoted 5 lines
xen/arch/arm/include/asm/pci.h +1 -0
@@ -155,6 +156,8 @@ bool arch_pci_device_physdevop(void);
 
 #else   /*!CONFIG_HAS_PCI*/
 
+#define pci_scan_enabled false

... this is entirely unused, and ...

xen/arch/arm/pci/pci.c bool arch_pci_device_physdevop(void) · line 95

Quoted 8 lines
xen/arch/arm/pci/pci.c +2 -0
@@ -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. */
+bool __ro_after_init pci_scan_enabled;

... and hence this wants to be static __initdata. Otherwise, strictly speaking, this is "unreachable data" post-init as per Misra's unreachable code criteria (which surely ought to extend to data as well).

xen/arch/arm/pci/pci.c static int __init pci_init(void) · line 118

Quoted 12 lines
xen/arch/arm/pci/pci.c +5 -2
@@ -104,9 +110,23 @@ 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 )
+        return ret;

I understand this is merely transforming existing code, but I don't see what actual use the returning of an error here and hence also ...

xen/arch/arm/pci/pci.c static int __init pci_init(void) · line 125

Quoted 6 lines
xen/arch/arm/pci/pci.c +6 -0
+    if ( pci_scan_enabled )
+    {
+        ret = scan_pci_devices();
+
+        if ( ret < 0 )
+            return ret;

... here has. The system will continue booting nevertheless, iirc without even a diagnostic from caller.

Jan

#3 lore raw

PATCH 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.

---
 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..cc0759023e 100644
@@ -50,7 +50,7 @@ int map_irq_to_domain(struct domain *d, unsigned int irq,
 }
 
 int map_range_to_domain(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 map_range_data *mr_data = data;
     struct domain *d = mr_data->d;
@@ -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..67447d8696 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;
             }
@@ -434,7 +434,7 @@ int __init pci_host_bridge_mappings(struct domain *d)
  * right place for alignment check.
  */
 static int is_bar_valid(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 pdev_bar_check *bar_data = data;
     paddr_t s = bar_data->start;
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.34.1
#4 lore raw

PATCH 3/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.

---
 docs/misc/xen-command-line.pandoc       |   4 +-
 xen/arch/arm/domain_build.c             | 151 +++++++++++++++++++++++-
 xen/arch/arm/include/asm/domain_build.h |   1 +
 xen/arch/arm/include/asm/pci.h          |  15 +++
 xen/arch/x86/include/asm/pci.h          |   6 +
 5 files changed, 175 insertions(+), 2 deletions(-)
docs/misc/xen-command-line.pandoc +3 -1
index 4a66c5a8f9..ac8e7bfb7a 100644
@@ -2077,7 +2077,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

xen/arch/arm/include/asm/domain_build.h +1 -0
index c6fec3168c..bb36e0150b 100644
@@ -6,6 +6,7 @@
 
 typedef __be32 gic_interrupt_t[3];
 int make_psci_node(void *fdt);
+int make_vpci_node(struct domain *d, void *fdt);
 void evtchn_allocate(struct domain *d);
 
 /*
xen/arch/arm/include/asm/pci.h +15 -0
index 7289f7688b..4fc46da315 100644
@@ -176,4 +176,19 @@ static inline int pci_get_new_domain_nr(void)
 }
 
 #endif  /*!CONFIG_HAS_PCI*/
+
+#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT
+static inline bool hwdom_uses_vpci(void)
+{
+    return pci_scan_enabled;
+}
+
+#else  /*!CONFIG_HAS_VPCI_GUEST_SUPPORT*/
+
+static inline bool hwdom_uses_vpci(void)
+{
+    return false;
+}
+#endif  /*!CONFIG_HAS_VPCI_GUEST_SUPPORT*/
+
 #endif /* __ARM_PCI_H__ */
xen/arch/x86/include/asm/pci.h +6 -0
index 0b98081aea..08e8959d0d 100644
@@ -76,4 +76,10 @@ int pci_sanitize_bar_memory(struct rangeset *r);
 
 void pci_setup(void);
 
+/* Unlike ARM, HW domain does not ever use vpci for x86 */
+static inline bool hwdom_uses_vpci(void)
+{
+    return false;
+}
+
 #endif /* __X86_PCI_H__ */
Signature · 1 line
2.34.1
#5 lore raw

Re: [PATCH v1 3/8] xen/pci: update DT for hwdom when it uses vpci

xen/arch/x86/include/asm/pci.h int pci_sanitize_bar_memory(struct rangeset *r); · line 79

Quoted 7 lines · Mykyta Poturai
xen/arch/x86/include/asm/pci.h +1 -0
@@ -76,4 +76,10 @@ int pci_sanitize_bar_memory(struct rangeset *r);
 
 void pci_setup(void);
 
+/* Unlike ARM, HW domain does not ever use vpci for x86 */

What did you derive this from? PVH Dom0 very well uses vPCI. In fact that's what it was first introduced for, iirc.

Jan

Quoted 6 lines
xen/arch/x86/include/asm/pci.h +5 -0
+static inline bool hwdom_uses_vpci(void)
+{
+    return false;
+}
+
 #endif /* __X86_PCI_H__ */
#6 lore raw

PATCH 4/8 xen/pci: disable pci_device_{add,remove} when hwdom uses vpci on arm

In reply to Mykyta Poturai

From: Luca Fancellu <[email protected]>

On ARM, if the hardware domain is using the emulated bus, it should not be allowed to add/remove pci devices, so return EOPNOTSUPP in that case.

---
 xen/drivers/pci/physdev.c | 6 ++++++
 1 file changed, 6 insertions(+)
xen/drivers/pci/physdev.c +6 -0
index d46501b884..5b3f8dde14 100644
@@ -19,6 +19,9 @@ ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         struct pci_dev_info pdev_info;
         nodeid_t node = NUMA_NO_NODE;
 
+        if ( hwdom_uses_vpci() )
+            return -EOPNOTSUPP;
+
         if ( !is_pci_passthrough_enabled() && !arch_pci_device_physdevop() )
             return -EOPNOTSUPP;
 
@@ -57,6 +60,9 @@ ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
     case PHYSDEVOP_pci_device_remove: {
         struct physdev_pci_device dev;
 
+        if ( hwdom_uses_vpci() )
+            return -EOPNOTSUPP;
+
         if ( !is_pci_passthrough_enabled() && !arch_pci_device_physdevop() )
             return -EOPNOTSUPP;
 
Signature · 1 line
2.34.1
#7 lore raw

Re: [PATCH v1 4/8] xen/pci: disable pci_device_{add,remove} when hwdom uses vpci on arm

Quoted 4 lines · Mykyta Poturai

From: Luca Fancellu <[email protected]>

On ARM, if the hardware domain is using the emulated bus, it should not be allowed to add/remove pci devices, so return EOPNOTSUPP in that case.

And how exactly would PCI hotplug be made work then?

Quoted 13 lines
xen/drivers/pci/physdev.c +3 -0
@@ -19,6 +19,9 @@ ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         struct pci_dev_info pdev_info;
         nodeid_t node = NUMA_NO_NODE;
 
+        if ( hwdom_uses_vpci() )
+            return -EOPNOTSUPP;
+
         if ( !is_pci_passthrough_enabled() && !arch_pci_device_physdevop() )
             return -EOPNOTSUPP;
 

xen/drivers/pci/physdev.c ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 68

Quoted 9 lines
xen/drivers/pci/physdev.c +3 -0
@@ -57,6 +60,9 @@ ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
     case PHYSDEVOP_pci_device_remove: {
         struct physdev_pci_device dev;
 
+        if ( hwdom_uses_vpci() )
+            return -EOPNOTSUPP;
+
         if ( !is_pci_passthrough_enabled() && !arch_pci_device_physdevop() )
             return -EOPNOTSUPP;
 

Along the lines of my comment on patch 3, these uses are clearly _visually_ wrong for x86; they're correct from a functional perspective simply because the predicate returns the (apparently) wrong value there right now.

Jan

#8 lore raw

PATCH 5/8 xen/pci: introduce has_vpci_bridge

In reply to Mykyta Poturai

From: Stefano Stabellini <[email protected]>

has_vpci_bridge is a macro to check if the domain is a domU or is dom0 with vPCI (pci-scan=yes) enabled.

Use the macro in drivers/vpci.

---
 xen/drivers/vpci/header.c | 14 +++++++-------
 xen/drivers/vpci/vpci.c   |  4 ++--
 xen/include/xen/vpci.h    |  9 +++++++++
 3 files changed, 18 insertions(+), 9 deletions(-)
xen/drivers/vpci/header.c +7 -7
index 469f497744..903168ff96 100644
@@ -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);
 
             return false;
@@ -492,7 +492,7 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)
             }
         }
 
-        if ( !is_hardware_domain(d) )
+        if ( has_vpci_bridge(d) )
             break;
 
         d = dom_xen;
@@ -522,7 +522,7 @@ static void cf_check cmd_write(
 {
     struct vpci_header *header = data;
 
-    if ( !is_hardware_domain(pdev->domain) )
+    if ( has_vpci_bridge(pdev->domain) )
     {
         const struct vpci *vpci = pdev->vpci;
 
@@ -564,7 +564,7 @@ static void cf_check bar_write(
     struct vpci_bar *bar = data;
     bool hi = false;
 
-    ASSERT(is_hardware_domain(pdev->domain));
+    ASSERT(!has_vpci_bridge(pdev->domain));
 
     if ( bar->type == VPCI_BAR_MEM64_HI )
     {
@@ -747,7 +747,7 @@ static int vpci_init_capability_list(struct pci_dev *pdev)
 {
     int rc;
     bool mask_cap_list = false;
-    bool is_hwdom = is_hardware_domain(pdev->domain);
+    bool is_hwdom = !has_vpci_bridge(pdev->domain);
 
     if ( pci_conf_read16(pdev->sbdf, PCI_STATUS) & PCI_STATUS_CAP_LIST )
     {
@@ -829,7 +829,7 @@ static int vpci_init_ext_capability_list(const struct pci_dev *pdev)
 {
     unsigned int pos = PCI_CFG_SPACE_SIZE;
 
-    if ( !is_hardware_domain(pdev->domain) )
+    if ( has_vpci_bridge(pdev->domain) )
         /* Extended capabilities read as zero, write ignore for DomU */
         return vpci_add_register(pdev->vpci, vpci_read_val, NULL,
                                  pos, 4, (void *)0);
@@ -866,7 +866,7 @@ int vpci_init_header(struct pci_dev *pdev)
     struct vpci_header *header = &pdev->vpci->header;
     struct vpci_bar *bars = header->bars;
     int rc;
-    bool is_hwdom = is_hardware_domain(pdev->domain);
+    bool is_hwdom = !has_vpci_bridge(pdev->domain);
 
     ASSERT(rw_is_write_locked(&pdev->domain->pci_lock));
 
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 +9 -0
index 9ae75d946a..e0aecfac72 100644
@@ -339,6 +339,15 @@ 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) || \
+                           (is_hardware_domain(d) && pci_scan_enabled))
+#else
+#define has_vpci_bridge(d) (!is_hardware_domain(d))
+#endif
+
 #endif
 
 /*
Signature · 1 line
2.34.1
#9 lore raw

Re: [PATCH v1 5/8] xen/pci: introduce has_vpci_bridge

Quoted 4 lines · Mykyta Poturai

From: Stefano Stabellini <[email protected]>

has_vpci_bridge is a macro to check if the domain is a domU or is dom0 with vPCI (pci-scan=yes) enabled.

Hmm. Why would DomU-s, now and forever, not have (virtual) bridges? Wasn't them gaining (virtual) bridges actually the longer-term plan?

Quoted 57 lines
xen/drivers/vpci/header.c +6 -6
@@ -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);
 
             return false;
@@ -492,7 +492,7 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)
             }
         }
 
-        if ( !is_hardware_domain(d) )
+        if ( has_vpci_bridge(d) )
             break;
 
         d = dom_xen;
@@ -522,7 +522,7 @@ static void cf_check cmd_write(
 {
     struct vpci_header *header = data;
 
-    if ( !is_hardware_domain(pdev->domain) )
+    if ( has_vpci_bridge(pdev->domain) )
     {
         const struct vpci *vpci = pdev->vpci;
 
@@ -564,7 +564,7 @@ static void cf_check bar_write(
     struct vpci_bar *bar = data;
     bool hi = false;
 
-    ASSERT(is_hardware_domain(pdev->domain));
+    ASSERT(!has_vpci_bridge(pdev->domain));
 
     if ( bar->type == VPCI_BAR_MEM64_HI )
     {
@@ -747,7 +747,7 @@ static int vpci_init_capability_list(struct pci_dev *pdev)
 {
     int rc;
     bool mask_cap_list = false;
-    bool is_hwdom = is_hardware_domain(pdev->domain);
+    bool is_hwdom = !has_vpci_bridge(pdev->domain);
 
     if ( pci_conf_read16(pdev->sbdf, PCI_STATUS) & PCI_STATUS_CAP_LIST )
     {
@@ -829,7 +829,7 @@ static int vpci_init_ext_capability_list(const struct pci_dev *pdev)
 {
     unsigned int pos = PCI_CFG_SPACE_SIZE;
 
-    if ( !is_hardware_domain(pdev->domain) )
+    if ( has_vpci_bridge(pdev->domain) )
         /* Extended capabilities read as zero, write ignore for DomU */
         return vpci_add_register(pdev->vpci, vpci_read_val, NULL,
                                  pos, 4, (void *)0);

xen/drivers/vpci/header.c int vpci_init_header(struct pci_dev *pdev) · line 871

Quoted 7 lines
xen/drivers/vpci/header.c +1 -1
@@ -866,7 +866,7 @@ int vpci_init_header(struct pci_dev *pdev)
     struct vpci_header *header = &pdev->vpci->header;
     struct vpci_bar *bars = header->bars;
     int rc;
-    bool is_hwdom = is_hardware_domain(pdev->domain);
+    bool is_hwdom = !has_vpci_bridge(pdev->domain);
 
     ASSERT(rw_is_write_locked(&pdev->domain->pci_lock));

For none of the changes (also further ones) it is clear (to me) why the substitution is (logically) correct. For this last instance the variable name also ends up wrong after the replacement.

xen/include/xen/vpci.h static inline int __must_check vpci_reset_device(struct pci_dev *pdev) · line 346

Quoted 11 lines
xen/include/xen/vpci.h +5 -0
@@ -339,6 +339,15 @@ 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) || \
+                           (is_hardware_domain(d) && pci_scan_enabled))

Nit: Off-by-1 indentation.

Jan

#10 lore raw

Re: [PATCH v1 5/8] xen/pci: introduce has_vpci_bridge

Quoted 6 lines · Mykyta Poturai
Quoted 4 lines

From: Stefano Stabellini <[email protected]>

has_vpci_bridge is a macro to check if the domain is a domU or is dom0 with vPCI (pci-scan=yes) enabled.

Hmm. Why would DomU-s, now and forever, not have (virtual) bridges?

Wasn't them

Quoted 2 lines

gaining (virtual) bridges actually the longer-term plan?

Seems like there is a misunderstanding here. Indeed it is the plan for DomUs to have virtual bridges and this check is designed to differentiate two categories of domains.
1. All DomUs + Dom0 with the virtual bridge.
2. Dom0 with HW bridge.

I will try to rephrase the commit message to be more clear.

Quoted 7 lines
Quoted 7 lines
xen/drivers/vpci/header.c +1 -1
@@ -866,7 +866,7 @@ int vpci_init_header(struct pci_dev *pdev)
      struct vpci_header *header = &pdev->vpci->header;
      struct vpci_bar *bars = header->bars;
      int rc;
-    bool is_hwdom = is_hardware_domain(pdev->domain);
+    bool is_hwdom = !has_vpci_bridge(pdev->domain);
 
Quoted 6 lines

Review int vpci_init_header(struct pci_dev *pdev) · line 871

Quoted 1 line
(quoted diff) +0 -0
      ASSERT(rw_is_write_locked(&pdev->domain->pci_lock));

For none of the changes (also further ones) it is clear (to me) why the substitution is (logically) correct. For this last instance the variable name also ends up wrong after the replacement.

Quoted 1 line

Jan

The general logic for this change is that before it we assumed that hwdom always uses HW bridge. Now it is not always true and hwdom can also use a virtual bridge, so it needs to be treated the same way as DomUs are.

You also mentioned on a previous series

Quoted 1 line

Here and perhaps everywhere else I wonder: Is this really an

appropriately

Quoted 1 line

named predicate for the purpose / context?

Maybe you have some ideas of a better name? From what I came up with this seems like the best one.

Signature · 1 line
Mykyta
#11 lore raw

Re: [PATCH v1 5/8] xen/pci: introduce has_vpci_bridge

Quoted 39 lines · Mykyta Poturai
Quoted 6 lines · Mykyta Poturai
Quoted 4 lines

From: Stefano Stabellini <[email protected]>

has_vpci_bridge is a macro to check if the domain is a domU or is dom0 with vPCI (pci-scan=yes) enabled.

Hmm. Why would DomU-s, now and forever, not have (virtual) bridges?

Wasn't them

Quoted 2 lines

gaining (virtual) bridges actually the longer-term plan?

Seems like there is a misunderstanding here. Indeed it is the plan for DomUs to have virtual bridges and this check is designed to differentiate two categories of domains.
1. All DomUs + Dom0 with the virtual bridge.
2. Dom0 with HW bridge.

I will try to rephrase the commit message to be more clear.

Quoted 7 lines
Quoted 7 lines
xen/drivers/vpci/header.c +1 -1
@@ -866,7 +866,7 @@ int vpci_init_header(struct pci_dev *pdev)
      struct vpci_header *header = &pdev->vpci->header;
      struct vpci_bar *bars = header->bars;
      int rc;
-    bool is_hwdom = is_hardware_domain(pdev->domain);
+    bool is_hwdom = !has_vpci_bridge(pdev->domain);
 
Quoted 6 lines

Review int vpci_init_header(struct pci_dev *pdev) · line 871

Quoted 1 line
(quoted diff) +0 -0
      ASSERT(rw_is_write_locked(&pdev->domain->pci_lock));

For none of the changes (also further ones) it is clear (to me) why the substitution is (logically) correct. For this last instance the variable name also ends up wrong after the replacement.

Quoted 1 line

Jan

The general logic for this change is that before it we assumed that hwdom always uses HW bridge. Now it is not always true and hwdom can also use a virtual bridge, so it needs to be treated the same way as DomUs are.

Quoted 8 lines

You also mentioned on a previous series

Quoted 1 line

Here and perhaps everywhere else I wonder: Is this really an

appropriately

Quoted 1 line

named predicate for the purpose / context?

Maybe you have some ideas of a better name? From what I came up with this seems like the best one.

How to name it depends on the (ultimate) purpose. If, as you say, it's intended to cover Dom0 and DomU uniformly when they have a virtual bridge, the name might be quite okay. Then, as you said you would do, better wording may be needed, variable names like the one mentioned above may need adjustment, and any code paths suddenly becoming usable for DomU-s need to be audited for this being a safe/secure thing to do (quite a few of the Dom0-only paths have been taking some liberties so far).

Jan

#12 lore raw

PATCH 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 distinct rangesets.

---
 xen/arch/arm/include/asm/pci.h     |  7 +++
 xen/arch/arm/pci/pci-host-common.c | 78 +++++++++++++++++++++++++++++
 xen/arch/x86/include/asm/pci.h     | 14 ++++++
 xen/common/rangeset.c              | 35 +++++++++++++
 xen/drivers/passthrough/pci.c      | 79 ++++++++++++++++++++++++++++++
 xen/include/xen/rangeset.h         |  6 ++-
 6 files changed, 218 insertions(+), 1 deletion(-)
xen/arch/arm/include/asm/pci.h +7 -0
index 4fc46da315..ba4caa56ae 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 +78 -0

102 lines not on this page · raw message

xen/arch/x86/include/asm/pci.h +14 -0
index 08e8959d0d..2cabbbb5d0 100644
@@ -82,4 +82,18 @@ static inline bool hwdom_uses_vpci(void)
     return false;
 }
 
+static inline uint64_t pci_get_new_bar_addr(const struct pci_dev *pdev,
+                                            uint64_t size, bool is_64bit,
+                                            bool prefetch)
+{
+    return 0;
+}
+
+static inline int pci_reserve_bar_range(const struct pci_dev *pdev,
+                                        uint64_t addr, uint64_t size,
+                                        bool prefetch)
+{
+    return 0;
+}
+
 #endif /* __X86_PCI_H__ */
xen/common/rangeset.c +35 -0
index 0e3b9acd35..c1c6f8610c 100644
@@ -357,6 +357,41 @@ int rangeset_claim_range(struct rangeset *r, unsigned long size,
     return 0;
 }
 
+int rangeset_find_aligned_range(struct rangeset *r, unsigned long size,
+                                unsigned long min, unsigned long *s)
+{
+    struct range *x;
+
+    /* Power of 2 check */
+    if ( (size & (size - 1)) != 0 )
+    {
+        *s = 0;
+        return -EINVAL;
+    }
+
+    read_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 = (x->s + size - 1) & ~(size - 1);
+
+        if ( x->e > start_aligned &&
+             (x->e - start_aligned) >= size &&
+             start_aligned >= min )
+        {
+            read_unlock(&r->lock);
+            *s = start_aligned;
+            return 0;
+        }
+    }
+
+    read_unlock(&r->lock);
+    *s = 0;
+
+    return -ENOSPC;
+}
+
 int rangeset_consume_ranges(struct rangeset *r,
                             int (*cb)(unsigned long s, unsigned long e,
                                       void *ctxt, unsigned long *c),
xen/drivers/passthrough/pci.c +79 -0

91 lines not on this page · raw message

xen/include/xen/rangeset.h +5 -1
index 817505badf..e71e810f82 100644
@@ -56,11 +56,15 @@ void rangeset_limit(
 bool __must_check rangeset_is_empty(
     const struct rangeset *r);
 
-/* Add/claim/remove/query/purge a numeric range. */
+/* Add/claim/find/remove/query/purge a numeric range. */
 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);
+int __must_check rangeset_find_aligned_range(struct rangeset *r,
+                                             unsigned long size,
+                                             unsigned long min,
+                                             unsigned long *s);
 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.34.1
#13 lore raw

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

Quoted 10 lines · 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.

Quoted 2 lines

Avaliable ranges are read from DT during init and stored in distinct rangesets.

The "distinct" lacks context here, imo. Maybe add "non-prefetchable and prefetchable" earlier in the sentence?

Quoted 11 lines
xen/arch/arm/pci/pci-host-common.c +1 -0
@@ -21,6 +21,7 @@
 #include <xen/rwlock.h>
 #include <xen/sched.h>
 #include <xen/vmap.h>
+#include <xen/resource.h>
 
 #include <asm/setup.h>
 

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

Quoted 5 lines
xen/arch/arm/pci/pci-host-common.c +2 -0
@@ -232,6 +233,21 @@ static int pci_bus_find_domain_nr(struct dt_device_node *dev)
     return domain;
 }
 
+static int add_bar_range(const struct dt_device_node *dev, uint32_t flags,
+                         uint64_t addr, uint64_t len, void *data)

Simply by its purpose, this function looks like it ought to be __init. And indeed, while its only caller - pci_host_common_probe() - isn't, all callers of that function are. Hence another prereq patch is wanted to add the missing __init there.

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

Quoted 13 lines
xen/arch/arm/pci/pci-host-common.c +8 -0
@@ -286,6 +302,14 @@ pci_host_common_probe(struct dt_device_node *dev,
     pci_add_host_bridge(bridge);
     pci_add_segment(bridge->segment);
 
+    bridge->bar_ranges = rangeset_new(NULL, "BAR ranges",
+                                      RANGESETF_prettyprint_hex);
+    bridge->bar_ranges_prefetch = rangeset_new(NULL,
+                                               "BAR ranges (prefetchable)",
+                                               RANGESETF_prettyprint_hex);
+    if ( bridge->bar_ranges && bridge->bar_ranges_prefetch )
+        dt_for_each_range(bridge->dt_node, add_bar_range, bridge);
+
     return bridge;

This is odd: Why would you silently ignore the -ENOMEM condition here? You can't blindly use the NULL pointer(s) ...

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

Quoted 7 lines
xen/arch/arm/pci/pci-host-common.c +3 -0
@@ -476,6 +500,60 @@ bool pci_check_bar(const struct pci_dev *pdev, mfn_t start, mfn_t end)
 
     return bar_data.is_valid;
 }
+
+uint64_t pci_get_new_bar_addr(const struct pci_dev *pdev, uint64_t size,
+                              bool is_64bit, bool prefetch)

(__init as well here)

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

Quoted 10 lines
xen/arch/arm/pci/pci-host-common.c +10 -0
+{
+    struct pci_host_bridge *bridge;
+    struct rangeset *range;
+    uint64_t addr;
+
+    bridge = pci_find_host_bridge(pdev->seg, pdev->bus);
+    if ( !bridge )
+        return 0;
+
+    range = prefetch ? bridge->bar_ranges_prefetch : bridge->bar_ranges;

... here, for example.

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

Quoted 10 lines
xen/arch/arm/pci/pci-host-common.c +10 -0
+    if ( size < PAGE_SIZE )
+        size = PAGE_SIZE;
+
+    if ( is_64bit && !rangeset_find_aligned_range(range, size, GB(4), &addr) )
+    {
+        if ( rangeset_remove_range(range, addr, addr + size - 1) )
+        {
+            printk("%s:%d:%s error\n", __FILE__, __LINE__, __func__);
+        }
+        return addr;

No need for the inner figure braces, but more importantly: printk()-style error handling is, well, insufficient? I'm pretty sure I indicated before that you can't assume the return value of rangeset_find_aligned_range() isn't stale the moment you look at (or use) it, unless you make further (written down) assumptions. For example, rangeset_find_aligned_range() becoming __init would already be a partial indication that it's not fit for general use.

__FILE__ / __LINE__ based diagnostics also aren't all this useful (and get in the way elsewhere, e.g. for livepatching).

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

Quoted 5 lines
xen/arch/arm/pci/pci-host-common.c +5 -0
+    }
+    if ( !rangeset_find_aligned_range(range, size, 0, &addr) )
+    {
+        if ( !is_64bit && addr >= GB(4) )
+            return 0;

What guarantees that no lower range is available? It's not said anywhere that the function would search addresses in increasing order.

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

Quoted 2 lines
xen/arch/arm/pci/pci-host-common.c +2 -0
+int pci_reserve_bar_range(const struct pci_dev *pdev, uint64_t addr,
+                          uint64_t size, bool prefetch)

__init again (I won't further repeat this)

xen/common/rangeset.c int rangeset_claim_range(struct rangeset *r, unsigned long size, · line 361

Quoted 8 lines
xen/common/rangeset.c +2 -0
@@ -357,6 +357,41 @@ int rangeset_claim_range(struct rangeset *r, unsigned long size,
     return 0;
 }
 
+int rangeset_find_aligned_range(struct rangeset *r, unsigned long size,
+                                unsigned long min, unsigned long *s)

What is "min" when there ought to be some alignment specified instead? (Reading the function body I understand that "size" also specifies the alignment, but that's not clear at all when looking at e.g. just the function declaration.)

How does the use of unsigned long here fit with the use of uint64_t in the callers?

Quoted 6 lines
xen/common/rangeset.c +6 -0
+{
+    struct range *x;
+
+    /* Power of 2 check */
+    if ( (size & (size - 1)) != 0 )
+    {

xen/common/rangeset.c int rangeset_claim_range(struct rangeset *r, unsigned long size, · line 377

Quoted 10 lines
xen/common/rangeset.c +10 -0
+        *s = 0;
+        return -EINVAL;
+    }
+
+    read_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 = (x->s + size - 1) & ~(size - 1);

The comment says what somehow you should actually assure: It _needs_ to be a power of 2 for this to work, yet size being 0 passes the check at the top of the function.

Also please don't open-code ROUNDUP().

Quoted 12 lines
xen/drivers/passthrough/pci.c +6 -0
@@ -1172,6 +1172,80 @@ int __init scan_pci_devices(void)
     return ret;
 }
 
+static void __init cf_check reserve_bar_range(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)) )

xen/drivers/passthrough/pci.c int __init scan_pci_devices(void) · line 1190

Quoted 10 lines
xen/drivers/passthrough/pci.c +10 -0
+        pci_reserve_bar_range(pdev, addr, size, prefetch);
+}
+
+static void __init cf_check get_new_bar_addr(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)) )
+    {

With how pci_check_bar() works on x86 right now, ...

Quoted 5 lines
xen/drivers/passthrough/pci.c +5 -0
+        uint16_t cmd = pci_conf_read16(pdev->sbdf, PCI_COMMAND);
+
+        addr = pci_get_new_bar_addr(pdev, size, is_64bit, prefetch);
+
+        pci_conf_write16(pdev->sbdf, PCI_COMMAND,

xen/drivers/passthrough/pci.c int __init scan_pci_devices(void) · line 1205

Quoted 10 lines
xen/drivers/passthrough/pci.c +10 -0
+                         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));
+
+        if ( is_64bit )
+            pci_conf_write32(pdev->sbdf, reg + 4, addr >> 32);
+
+        pci_conf_write16(pdev->sbdf, PCI_COMMAND, cmd);

... all of this is unreachable code there. Misra doesn't like such.

Furthermore x86'es variant emits diagnostics, and aiui we'd now see them more frequently (possibly: than wanted).

xen/drivers/passthrough/pci.c int __init scan_pci_devices(void) · line 1214

Quoted 6 lines
xen/drivers/passthrough/pci.c +6 -0
+static int __init cf_check bars_iterate(struct pci_seg *pseg, void *arg)
+{
+    struct pci_dev *pdev;
+    unsigned int i, ret, num_bars = PCI_HEADER_NORMAL_NR_BARS;
+    uint64_t addr, size;
+    void (*cb)(struct pci_dev *, uint8_t, uint64_t, uint64_t, bool, bool) = arg;

There is still no connection at all between this function pointer type variable and the two functions tobe called? Why is "cb" not directly the function parameter? (For Misra's sake it may also be necessary to name the six parameters; I'm not entirely sure, though. There was some back and forth at the time.)

Also, why is the first parameter not pointer-to-const?

xen/drivers/passthrough/pci.c int __init scan_pci_devices(void) · line 1227

Quoted 12 lines
xen/drivers/passthrough/pci.c +12 -0
+    list_for_each_entry ( pdev, &pseg->alldevs_list, alldevs_list )
+    {
+        if ( (pci_conf_read8(pdev->sbdf, PCI_HEADER_TYPE) & 0x7f) ==
+             PCI_HEADER_TYPE_NORMAL )
+        {
+            for ( i = 0; i < num_bars; i += ret )
+            {
+                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 )

Nit (style): Operator placement.

xen/drivers/passthrough/pci.c int __init scan_pci_devices(void) · line 1234

Quoted 7 lines
xen/drivers/passthrough/pci.c +7 -0
+                {
+                    ret = 1;
+                    continue;
+                }
+
+                ret = pci_size_mem_bar(pdev->sbdf, reg, &addr, &size,
+                                      (i == num_bars - 1) ? PCI_BAR_LAST : 0);

Nit (style): Indentation.

xen/drivers/passthrough/pci.c int __init scan_pci_devices(void) · line 1239

Quoted 4 lines
xen/drivers/passthrough/pci.c +4 -0
+                if ( !size )
+                    continue;
+                prefetch = !!(pci_conf_read32(pdev->sbdf, reg) &
+                              PCI_BASE_ADDRESS_MEM_PREFETCH);

No need for !!.

xen/drivers/passthrough/pci.c void __hwdom_init setup_hwdom_pci_devices( · line 1346

Quoted 11 lines
xen/drivers/passthrough/pci.c +5 -0
@@ -1263,6 +1337,11 @@ void __hwdom_init setup_hwdom_pci_devices(
     struct setup_hwdom ctxt = { .d = d, .handler = handler };
 
     pcidevs_lock();
+    if ( hwdom_uses_vpci() )
+    {
+        pci_segments_iterate(bars_iterate, reserve_bar_range);
+        pci_segments_iterate(bars_iterate, get_new_bar_addr);
+    }
     pci_segments_iterate(_setup_hwdom_pci_devices, &ctxt);
     pcidevs_unlock();

Does this really need splitting into 3 steps? Can't _setup_hwdom_pci_devices() take care of the uninitialized BARs?

Jan

#14 lore raw

PATCH 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.

---
 xen/drivers/passthrough/arm/iommu.c |  9 +++++++
 xen/drivers/passthrough/pci.c       | 40 ++++++++++++++++++++++++++++-
 2 files changed, 48 insertions(+), 1 deletion(-)
xen/drivers/passthrough/arm/iommu.c +9 -0
index 100545e23f..d110520e0d 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,12 @@ void arch_iommu_domain_destroy(struct domain *d)
 {
 }
 
+static int 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));
+}
+
 void __hwdom_init arch_iommu_hwdom_init(struct domain *d)
 {
     /* Set to false options not supported on ARM. */
@@ -142,6 +149,8 @@ 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;
+
+    setup_hwdom_pci_devices(d, iommu_add_hwdom_pci_device);
 }
 
 /*
xen/drivers/passthrough/pci.c +39 -1
index 4f5de9a542..534faaaa84 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>
@@ -1040,6 +1041,12 @@ enum pdev_type pdev_type(u16 seg, u8 bus, u8 devfn)
     return pos ? DEV_TYPE_PCIe_ENDPOINT : DEV_TYPE_PCI;
 }
 
+static bool pdev_is_endpoint(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
@@ -1255,7 +1262,7 @@ static void __hwdom_init setup_one_hwdom_device(const struct setup_hwdom *ctxt,
                                                 struct pci_dev *pdev)
 {
     u8 devfn = pdev->devfn;
-    int err;
+    int err, i, rc;
 
     do {
         err = ctxt->handler(devfn, pdev);
@@ -1276,6 +1283,34 @@ static void __hwdom_init setup_one_hwdom_device(const struct setup_hwdom *ctxt,
     if ( err )
         printk(XENLOG_ERR "setup of vPCI for d%d failed: %d\n",
                ctxt->d->domain_id, err);
+
+    if ( !hwdom_uses_vpci() )
+        return;
+
+    for ( i = 0; i < PCI_HEADER_NORMAL_NR_BARS; i += rc )
+    {
+        uint64_t addr, size;
+        uint8_t reg = PCI_BASE_ADDRESS_0 + i * 4;
+
+        if ( (pci_conf_read32(pdev->sbdf, reg) & PCI_BASE_ADDRESS_SPACE)
+             == PCI_BASE_ADDRESS_SPACE_IO )
+        {
+            rc = 1;
+            continue;
+        }
+
+        rc = pci_size_mem_bar(pdev->sbdf, reg, &addr, &size,
+                              (i == PCI_HEADER_NORMAL_NR_BARS - 1)
+                                  ? PCI_BAR_LAST : 0);
+
+        if ( !size )
+            continue;
+
+        err = iomem_permit_access(hardware_domain, paddr_to_pfn(addr),
+                             paddr_to_pfn(PAGE_ALIGN(addr + size - 1)));
+        if ( err )
+            break;
+    }
 }
 
 static int __hwdom_init cf_check _setup_hwdom_pci_devices(
@@ -1294,6 +1329,9 @@ static int __hwdom_init cf_check _setup_hwdom_pci_devices(
             if ( !pdev )
                 continue;
 
+            if ( hwdom_uses_vpci() && !pdev_is_endpoint(pdev) )
+                continue;
+
             if ( !pdev->domain )
             {
                 pdev->domain = ctxt->d;
Signature · 1 line
2.34.1
#15 lore raw

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

xen/drivers/passthrough/arm/iommu.c void arch_iommu_domain_destroy(struct domain *d) · line 140

Quoted 8 lines · Mykyta Poturai
xen/drivers/passthrough/arm/iommu.c +4 -0
@@ -133,6 +134,12 @@ void arch_iommu_domain_destroy(struct domain *d)
 {
 }
 
+static int 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));

Nit (style): Blank line please between declaration(s) and statement(s). And blank line please also ahead of the main return statement of a function.

xen/drivers/passthrough/arm/iommu.c void __hwdom_init arch_iommu_hwdom_init(struct domain *d) · line 153

Quoted 6 lines
xen/drivers/passthrough/arm/iommu.c +2 -0
@@ -142,6 +149,8 @@ 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;
+
+    setup_hwdom_pci_devices(d, iommu_add_hwdom_pci_device);

With this function being __hwdom_init, why is iommu_add_hwdom_pci_device() not also given that attribute?

As to cf_check I don't know what the Arm policy is: My suggestion would be to put that attribute wherever it is (potentially) needed.

Quoted 11 lines
xen/drivers/passthrough/pci.c +1 -0
@@ -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>

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

Quoted 4 lines
xen/drivers/passthrough/pci.c +1 -0
@@ -1040,6 +1041,12 @@ enum pdev_type pdev_type(u16 seg, u8 bus, u8 devfn)
     return pos ? DEV_TYPE_PCIe_ENDPOINT : DEV_TYPE_PCI;
 }
 
+static bool pdev_is_endpoint(struct pci_dev *pdev)

__hwdom_init? And parameter pointer-to-const?

Quoted 9 lines
xen/drivers/passthrough/pci.c +5 -0
+{
+    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

xen/drivers/passthrough/pci.c static void __hwdom_init setup_one_hwdom_device(const struct setup_hwdom *ctxt, · line 1265

Quoted 5 lines
xen/drivers/passthrough/pci.c +1 -1
@@ -1255,7 +1262,7 @@ static void __hwdom_init setup_one_hwdom_device(const struct setup_hwdom *ctxt,
                                                 struct pci_dev *pdev)
 {
     u8 devfn = pdev->devfn;
-    int err;
+    int err, i, rc;

i clearly wants to be of an unsigned type. And rc, afaics, can have its scope limited to the loop body.

Quoted 5 lines
xen/drivers/passthrough/pci.c +1 -0
@@ -1276,6 +1283,34 @@ static void __hwdom_init setup_one_hwdom_device(const struct setup_hwdom *ctxt,
     if ( err )
         printk(XENLOG_ERR "setup of vPCI for d%d failed: %d\n",
                ctxt->d->domain_id, err);
+

xen/drivers/passthrough/pci.c static void __hwdom_init setup_one_hwdom_device(const struct setup_hwdom *ctxt, · line 1296

Quoted 10 lines
xen/drivers/passthrough/pci.c +10 -0
+    if ( !hwdom_uses_vpci() )
+        return;
+
+    for ( i = 0; i < PCI_HEADER_NORMAL_NR_BARS; i += rc )
+    {
+        uint64_t addr, size;
+        uint8_t reg = PCI_BASE_ADDRESS_0 + i * 4;
+
+        if ( (pci_conf_read32(pdev->sbdf, reg) & PCI_BASE_ADDRESS_SPACE)
+             == PCI_BASE_ADDRESS_SPACE_IO )

Nit (style): Operator placement again.

xen/drivers/passthrough/pci.c static void __hwdom_init setup_one_hwdom_device(const struct setup_hwdom *ctxt, · line 1304

Quoted 8 lines
xen/drivers/passthrough/pci.c +8 -0
+        {
+            rc = 1;
+            continue;
+        }
+
+        rc = pci_size_mem_bar(pdev->sbdf, reg, &addr, &size,
+                              (i == PCI_HEADER_NORMAL_NR_BARS - 1)
+                                  ? PCI_BAR_LAST : 0);

Nit (style): Indentation again.

xen/drivers/passthrough/pci.c static void __hwdom_init setup_one_hwdom_device(const struct setup_hwdom *ctxt, · line 1310

Quoted 6 lines
xen/drivers/passthrough/pci.c +6 -0
+
+        if ( !size )
+            continue;
+
+        err = iomem_permit_access(hardware_domain, paddr_to_pfn(addr),
+                             paddr_to_pfn(PAGE_ALIGN(addr + size - 1)));

And again.

xen/drivers/passthrough/pci.c static void __hwdom_init setup_one_hwdom_device(const struct setup_hwdom *ctxt, · line 1314

Quoted 4 lines
xen/drivers/passthrough/pci.c +3 -0
+        if ( err )
+            break;
+    }
 }

This change supports my comment on the earlier patch, regarding the option of doing here some of what needs doing, rather than by another round of iterating all devices.

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

Quoted 10 lines
xen/drivers/passthrough/pci.c +3 -0
@@ -1294,6 +1329,9 @@ static int __hwdom_init cf_check _setup_hwdom_pci_devices(
             if ( !pdev )
                 continue;
 
+            if ( hwdom_uses_vpci() && !pdev_is_endpoint(pdev) )
+                continue;
+
             if ( !pdev->domain )
             {
                 pdev->domain = ctxt->d;

This is (logically) wrong: On x86 PVH Dom0 uses vPCI but wants all devices to be handed to it. _This_ may be a place where has_vpci_bridge() might make sense to use (simply by its name, that is).

Jan

#16 lore raw

PATCH 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 reflrect this change.

---
 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..dbe3347cea 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) && hwdom_uses_vpci())
 
 struct arch_vcpu_io {
     struct instr_details dabt_instr; /* when the instruction is decoded */
Signature · 1 line
2.34.1
#17 lore raw

Re: [PATCH v1 8/8] arm/pci: enable vpci for hwdom when pci-scan is enabled

Quoted 2 lines · Mykyta Poturai

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

Small typo in commit message "reflect".

Quoted 20 lines
---
  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..dbe3347cea 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) && hwdom_uses_vpci())
 
  struct arch_vcpu_io {
      struct instr_details dabt_instr; /* when the instruction is decoded */
Signature · 6 lines
--
Yann Sionneau | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech