Skip to contentSkip to conversation

xen/pci: implement is_memory_hole for ARM

PATCH Superseded Rahul Singh 5 participants 16 messages Sep 1, 2022 – Sep 7, 2022

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 cover xen/pci: implement is_memory_hole for ARM

This patch series is to implement something like is_memory_hole function for ARM.

Rahul Singh (2):
xen/arm: pci: modify pci_find_host_bridge_node argument to const pdev xen/pci: replace call to is_memory_hole to pci_check_bar

 xen/arch/arm/include/asm/pci.h     |  5 +++-
 xen/arch/arm/pci/pci-host-common.c | 47 ++++++++++++++++++++++++++++--
 xen/arch/x86/include/asm/pci.h     | 10 +++++++
 xen/drivers/passthrough/pci.c      |  8 ++---
 4 files changed, 63 insertions(+), 7 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v3 1/2 xen/arm: pci: modify pci_find_host_bridge_node argument to const pdev

Modify pci_find_host_bridge_node argument to const pdev to avoid converting the dev to pdev in pci_find_host_bridge_node and also constify the return.

---
Changes in v3:
- no changes

---
 xen/arch/arm/include/asm/pci.h     | 3 ++-
 xen/arch/arm/pci/pci-host-common.c | 4 ++--
 2 files changed, 4 insertions(+), 3 deletions(-)
xen/arch/arm/include/asm/pci.h +2 -1
index 7c7449d64f..80a2431804 100644
@@ -106,7 +106,8 @@ bool pci_ecam_need_p2m_hwdom_mapping(struct domain *d,
                                      struct pci_host_bridge *bridge,
                                      uint64_t addr);
 struct pci_host_bridge *pci_find_host_bridge(uint16_t segment, uint8_t bus);
-struct dt_device_node *pci_find_host_bridge_node(struct device *dev);
+const struct dt_device_node *
+pci_find_host_bridge_node(const struct pci_dev *pdev);
 int pci_get_host_bridge_segment(const struct dt_device_node *node,
                                 uint16_t *segment);
 
xen/arch/arm/pci/pci-host-common.c +2 -2
index fd8c0f837a..89ef30028e 100644
@@ -243,10 +243,10 @@ err_exit:
 /*
  * Get host bridge node given a device attached to it.
  */
-struct dt_device_node *pci_find_host_bridge_node(struct device *dev)
+const struct dt_device_node *
+pci_find_host_bridge_node(const struct pci_dev *pdev)
 {
     struct pci_host_bridge *bridge;
-    struct pci_dev *pdev = dev_to_pci(dev);
 
     bridge = pci_find_host_bridge(pdev->seg, pdev->bus);
     if ( unlikely(!bridge) )
Signature · 1 line
2.25.1
#2 lore raw

PATCH v3 2/2 xen/pci: replace call to is_memory_hole to pci_check_bar

In reply to Rahul Singh

is_memory_hole was implemented for x86 and not for ARM when introduced. Replace is_memory_hole call to pci_check_bar as function should check if device BAR is in defined memory range. Also, add an implementation for ARM which is required for PCI passthrough.

On x86, pci_check_bar will call is_memory_hole which will check if BAR is not overlapping with any memory region defined in the memory map.

On ARM, pci_check_bar will go through the host bridge ranges and check if the BAR is in the range of defined ranges.

---
Changes in v3:
- fix minor comments

---
 xen/arch/arm/include/asm/pci.h     |  2 ++
 xen/arch/arm/pci/pci-host-common.c | 43 ++++++++++++++++++++++++++++++
 xen/arch/x86/include/asm/pci.h     | 10 +++++++
 xen/drivers/passthrough/pci.c      |  8 +++---
 4 files changed, 59 insertions(+), 4 deletions(-)
xen/arch/arm/include/asm/pci.h +2 -0
index 80a2431804..8cb46f6b71 100644
@@ -126,6 +126,8 @@ int pci_host_iterate_bridges_and_count(struct domain *d,
 
 int pci_host_bridge_mappings(struct domain *d);
 
+bool pci_check_bar(const struct pci_dev *pdev, mfn_t start, mfn_t end);
+
 #else   /*!CONFIG_HAS_PCI*/
 
 struct arch_pci_dev { };
xen/arch/arm/pci/pci-host-common.c +43 -0

55 lines not on this page · raw message

xen/arch/x86/include/asm/pci.h +10 -0
index c8e1a9ecdb..f4a58c8acf 100644
@@ -57,4 +57,14 @@ static always_inline bool is_pci_passthrough_enabled(void)
 
 void arch_pci_init_pdev(struct pci_dev *pdev);
 
+static inline bool pci_check_bar(const struct pci_dev *pdev,
+                                 mfn_t start, mfn_t end)
+{
+    /*
+     * Check if BAR is not overlapping with any memory region defined
+     * in the memory map.
+     */
+    return is_memory_hole(start, end);
+}
+
 #endif /* __X86_PCI_H__ */
xen/drivers/passthrough/pci.c +4 -4
index cdaf5c247f..149f68bb6e 100644
@@ -304,8 +304,8 @@ static void check_pdev(const struct pci_dev *pdev)
         if ( rc < 0 )
             /* Unable to size, better leave memory decoding disabled. */
             return;
-        if ( size && !is_memory_hole(maddr_to_mfn(addr),
-                                     maddr_to_mfn(addr + size - 1)) )
+        if ( size && !pci_check_bar(pdev, maddr_to_mfn(addr),
+                                    maddr_to_mfn(addr + size - 1)) )
         {
             /*
              * Return without enabling memory decoding if BAR position is not
@@ -331,8 +331,8 @@ static void check_pdev(const struct pci_dev *pdev)
 
         if ( rc < 0 )
             return;
-        if ( size && !is_memory_hole(maddr_to_mfn(addr),
-                                     maddr_to_mfn(addr + size - 1)) )
+        if ( size && !pci_check_bar(pdev, maddr_to_mfn(addr),
+                                    maddr_to_mfn(addr + size - 1)) )
         {
             printk(warn, &pdev->sbdf, "ROM ", PFN_DOWN(addr),
                    PFN_DOWN(addr + size - 1));
Signature · 1 line
2.25.1
Reviewed-by Oleksandr Tyshchenko on v3 2/2
#3 lore raw

Re: [PATCH v3 2/2] xen/pci: replace call to is_memory_hole to pci_check_bar

On 01.09.22 12:29, Rahul Singh wrote:

Hello Rahul

Quoted 5 lines

is_memory_hole was implemented for x86 and not for ARM when introduced. Replace is_memory_hole call to pci_check_bar as function should check if device BAR is in defined memory range. Also, add an implementation for ARM which is required for PCI passthrough.

Quoted 7 lines

On x86, pci_check_bar will call is_memory_hole which will check if BAR is not overlapping with any memory region defined in the memory map.

On ARM, pci_check_bar will go through the host bridge ranges and check if the BAR is in the range of defined ranges.

Thanks!

Quoted 129 lines

---
Changes in v3:
- fix minor comments

---
  xen/arch/arm/include/asm/pci.h     |  2 ++
  xen/arch/arm/pci/pci-host-common.c | 43 ++++++++++++++++++++++++++++++
  xen/arch/x86/include/asm/pci.h     | 10 +++++++
  xen/drivers/passthrough/pci.c      |  8 +++---
  4 files changed, 59 insertions(+), 4 deletions(-)
xen/arch/arm/include/asm/pci.h +2 -0
index 80a2431804..8cb46f6b71 100644
@@ -126,6 +126,8 @@ int pci_host_iterate_bridges_and_count(struct domain *d,
 
  int pci_host_bridge_mappings(struct domain *d);
 
+bool pci_check_bar(const struct pci_dev *pdev, mfn_t start, mfn_t end);
+
  #else   /*!CONFIG_HAS_PCI*/
 
  struct arch_pci_dev { };
xen/arch/arm/pci/pci-host-common.c +43 -0
index 89ef30028e..0eb121666d 100644
@@ -24,6 +24,16 @@
 
  #include <asm/setup.h>
 
+/*
+ * struct to hold pci device bar.
+ */
+struct pdev_bar
+{
+    mfn_t start;
+    mfn_t end;
+    bool is_valid;
+};
+
  /*
   * List for all the pci host bridges.
   */
@@ -363,6 +373,39 @@ int __init pci_host_bridge_mappings(struct domain *d)
      return 0;
  }
 
+static int is_bar_valid(const struct dt_device_node *dev,
+                        uint64_t addr, uint64_t len, void *data)
+{
+    struct pdev_bar *bar_data = data;
+    unsigned long s = mfn_x(bar_data->start);
+    unsigned long e = mfn_x(bar_data->end);
+
+    if ( (s <= e) && (s >= PFN_DOWN(addr)) && (e <= PFN_UP(addr + len - 1)) )
+        bar_data->is_valid =  true;
+
+    return 0;
+}
+
+bool pci_check_bar(const struct pci_dev *pdev, mfn_t start, mfn_t end)
+{
+    int ret;
+    const struct dt_device_node *dt_node;
+    struct pdev_bar bar_data =  {
+        .start = start,
+        .end = end,
+        .is_valid = false
+    };
+
+    dt_node = pci_find_host_bridge_node(pdev);
+    if ( !dt_node )
+        return false;
+
+    ret = dt_for_each_range(dt_node, &is_bar_valid, &bar_data);
+    if ( ret < 0 )
+        return false;
+
+    return bar_data.is_valid;
+}
  /*
   * Local variables:
   * mode: C
xen/arch/x86/include/asm/pci.h +10 -0
index c8e1a9ecdb..f4a58c8acf 100644
@@ -57,4 +57,14 @@ static always_inline bool is_pci_passthrough_enabled(void)
 
  void arch_pci_init_pdev(struct pci_dev *pdev);
 
+static inline bool pci_check_bar(const struct pci_dev *pdev,
+                                 mfn_t start, mfn_t end)
+{
+    /*
+     * Check if BAR is not overlapping with any memory region defined
+     * in the memory map.
+     */
+    return is_memory_hole(start, end);
+}
+
  #endif /* __X86_PCI_H__ */
xen/drivers/passthrough/pci.c +4 -4
index cdaf5c247f..149f68bb6e 100644
@@ -304,8 +304,8 @@ static void check_pdev(const struct pci_dev *pdev)
          if ( rc < 0 )
              /* Unable to size, better leave memory decoding disabled. */
              return;
-        if ( size && !is_memory_hole(maddr_to_mfn(addr),
-                                     maddr_to_mfn(addr + size - 1)) )
+        if ( size && !pci_check_bar(pdev, maddr_to_mfn(addr),
+                                    maddr_to_mfn(addr + size - 1)) )
          {
              /*
               * Return without enabling memory decoding if BAR position is not
@@ -331,8 +331,8 @@ static void check_pdev(const struct pci_dev *pdev)
 
          if ( rc < 0 )
              return;
-        if ( size && !is_memory_hole(maddr_to_mfn(addr),
-                                     maddr_to_mfn(addr + size - 1)) )
+        if ( size && !pci_check_bar(pdev, maddr_to_mfn(addr),
+                                    maddr_to_mfn(addr + size - 1)) )
          {
              printk(warn, &pdev->sbdf, "ROM ", PFN_DOWN(addr),
                     PFN_DOWN(addr + size - 1));
Signature · 3 lines
Regards,

Oleksandr Tyshchenko
#4 lore raw

Re: [PATCH v3 2/2] xen/pci: replace call to is_memory_hole to pci_check_bar

In reply to Rahul Singh
Quoted 58 lines · Rahul Singh

Replace is_memory_hole call to pci_check_bar as function should check if device BAR is in defined memory range. Also, add an implementation for ARM which is required for PCI passthrough.

On x86, pci_check_bar will call is_memory_hole which will check if BAR is not overlapping with any memory region defined in the memory map.

On ARM, pci_check_bar will go through the host bridge ranges and check if the BAR is in the range of defined ranges.

---
Changes in v3:
- fix minor comments

---
 xen/arch/arm/include/asm/pci.h     |  2 ++
 xen/arch/arm/pci/pci-host-common.c | 43 ++++++++++++++++++++++++++++++
 xen/arch/x86/include/asm/pci.h     | 10 +++++++
 xen/drivers/passthrough/pci.c      |  8 +++---
 4 files changed, 59 insertions(+), 4 deletions(-)
xen/arch/arm/include/asm/pci.h +2 -0
index 80a2431804..8cb46f6b71 100644
@@ -126,6 +126,8 @@ int pci_host_iterate_bridges_and_count(struct domain *d,
 
 int pci_host_bridge_mappings(struct domain *d);
 
+bool pci_check_bar(const struct pci_dev *pdev, mfn_t start, mfn_t end);
+
 #else   /*!CONFIG_HAS_PCI*/
 
 struct arch_pci_dev { };
xen/arch/arm/pci/pci-host-common.c +10 -0
index 89ef30028e..0eb121666d 100644
@@ -24,6 +24,16 @@
 
 #include <asm/setup.h>
 
+/*
+ * struct to hold pci device bar.
+ */
+struct pdev_bar
+{
+    mfn_t start;
+    mfn_t end;
+    bool is_valid;
+};
+
 /*
  * List for all the pci host bridges.
  */
@@ -363,6 +373,39 @@ int __init pci_host_bridge_mappings(struct domain *d)
     return 0;
 }

xen/arch/arm/pci/pci-host-common.c int __init pci_host_bridge_mappings(struct domain *d) · line 384

Quoted 10 lines
xen/arch/arm/pci/pci-host-common.c +9 -0
 
+static int is_bar_valid(const struct dt_device_node *dev,
+                        uint64_t addr, uint64_t len, void *data)
+{
+    struct pdev_bar *bar_data = data;
+    unsigned long s = mfn_x(bar_data->start);
+    unsigned long e = mfn_x(bar_data->end);
+
+    if ( (s <= e) && (s >= PFN_DOWN(addr)) && (e <= PFN_UP(addr + len - 1)) )
+        bar_data->is_valid =  true;

This patch looks good and you addressed all Jan's comment well. Before I ack it, one question.

I know that you made this change to address Jan's comment but using PFN_DOWN for the (s >= PFN_DOWN(addr)) check and PFN_UP for the (e <= PFN_UP(addr + len - 1)) check means that we are relaxing the requirements, aren't we?

I know that this discussion is a bit pointless because addr and len should always be page aligned, and if they weren't it would be a mistake. But assuming that they are not page aligned, wouldn't we want this check to be a strict as possible?

Wouldn't we want to ensure that the [s,e] range is a strict subset of [addr,addr+len-1] ? If so we would need to do the following instead:

    if ( (s <= e) && (s >= PFN_UP(addr)) && (e <= PFN_DOWN(addr + len - 1)) )
        bar_data->is_valid =  true;
#11 lore raw

Re: [PATCH v3 2/2] xen/pci: replace call to is_memory_hole to pci_check_bar

In reply to Rahul Singh

Hi Rahul,

Quoted 40 lines · Rahul Singh

is_memory_hole was implemented for x86 and not for ARM when introduced. Replace is_memory_hole call to pci_check_bar as function should check if device BAR is in defined memory range. Also, add an implementation for ARM which is required for PCI passthrough.

On x86, pci_check_bar will call is_memory_hole which will check if BAR is not overlapping with any memory region defined in the memory map.

On ARM, pci_check_bar will go through the host bridge ranges and check if the BAR is in the range of defined ranges.

---
Changes in v3:
- fix minor comments

---
  xen/arch/arm/include/asm/pci.h     |  2 ++
  xen/arch/arm/pci/pci-host-common.c | 43 ++++++++++++++++++++++++++++++
  xen/arch/x86/include/asm/pci.h     | 10 +++++++
  xen/drivers/passthrough/pci.c      |  8 +++---
  4 files changed, 59 insertions(+), 4 deletions(-)
xen/arch/arm/include/asm/pci.h +2 -0
index 80a2431804..8cb46f6b71 100644
@@ -126,6 +126,8 @@ int pci_host_iterate_bridges_and_count(struct domain *d,
 
  int pci_host_bridge_mappings(struct domain *d);
 
+bool pci_check_bar(const struct pci_dev *pdev, mfn_t start, mfn_t end);
+
  #else   /*!CONFIG_HAS_PCI*/
 
  struct arch_pci_dev { };
xen/arch/arm/pci/pci-host-common.c +0 -0
index 89ef30028e..0eb121666d 100644

xen/arch/arm/pci/pci-host-common.c line 29

Quoted 6 lines
xen/arch/arm/pci/pci-host-common.c +3 -0
@@ -24,6 +24,16 @@
 
  #include <asm/setup.h>
 
+/*
+ * struct to hold pci device bar.
+ */

I find this comment a bit misleading. What you are storing is a candidate region. IOW, this may or may not be a PCI device bar.

Given the current use below, I would rename the structure to something more specific like: pdev_bar_check.

Quoted 12 lines
xen/arch/arm/pci/pci-host-common.c +7 -0
+struct pdev_bar
+{
+    mfn_t start;
+    mfn_t end;
+    bool is_valid;
+};
+
  /*
   * List for all the pci host bridges.
   */
@@ -363,6 +373,39 @@ int __init pci_host_bridge_mappings(struct domain *d)
      return 0;

xen/arch/arm/pci/pci-host-common.c int __init pci_host_bridge_mappings(struct domain *d) · line 383

Quoted 10 lines
xen/arch/arm/pci/pci-host-common.c +8 -0
  }
 
+static int is_bar_valid(const struct dt_device_node *dev,
+                        uint64_t addr, uint64_t len, void *data)
+{
+    struct pdev_bar *bar_data = data;
+    unsigned long s = mfn_x(bar_data->start);
+    unsigned long e = mfn_x(bar_data->end);
+
+    if ( (s <= e) && (s >= PFN_DOWN(addr)) && (e <= PFN_UP(addr + len - 1)) )

AFAICT 's' and 'e' are provided by pci_check_bar() and will never change. So can we move the check 's <= e' outside of the callback?

xen/arch/arm/pci/pci-host-common.c int __init pci_host_bridge_mappings(struct domain *d) · line 390

Quoted 7 lines
xen/arch/arm/pci/pci-host-common.c +7 -0
+        bar_data->is_valid =  true;
+
+    return 0;
+}
+
+bool pci_check_bar(const struct pci_dev *pdev, mfn_t start, mfn_t end)
+{

Other than the current calls in check_pdev(), do you have plan to use it in more places? The reason I am asking it is this function is non-trivial on Arm (dt_for_each_range() is quite complex).

Cheers,

Signature · 1 line
Julien Grall
Acked-by on v3 2/2
#15 lore raw

Re: [PATCH v3 2/2] xen/pci: replace call to is_memory_hole to pci_check_bar

In reply to Rahul Singh
Quoted 5 lines · Rahul Singh

is_memory_hole was implemented for x86 and not for ARM when introduced. Replace is_memory_hole call to pci_check_bar as function should check if device BAR is in defined memory range. Also, add an implementation for ARM which is required for PCI passthrough.

Quoted 7 lines

On x86, pci_check_bar will call is_memory_hole which will check if BAR is not overlapping with any memory region defined in the memory map.

On ARM, pci_check_bar will go through the host bridge ranges and check if the BAR is in the range of defined ranges.

#12 lore raw

Re: [PATCH v3 2/2] xen/pci: replace call to is_memory_hole to pci_check_bar

In reply to Julien Grall

Hi Julien,

Quoted 46 lines · Julien Grall

Hi Rahul,

Quoted 33 lines · Rahul Singh

is_memory_hole was implemented for x86 and not for ARM when introduced. Replace is_memory_hole call to pci_check_bar as function should check if device BAR is in defined memory range. Also, add an implementation for ARM which is required for PCI passthrough.
On x86, pci_check_bar will call is_memory_hole which will check if BAR is not overlapping with any memory region defined in the memory map. On ARM, pci_check_bar will go through the host bridge ranges and check if the BAR is in the range of defined ranges.

---
Changes in v3:
- fix minor comments

---
 xen/arch/arm/include/asm/pci.h     |  2 ++
 xen/arch/arm/pci/pci-host-common.c | 43 ++++++++++++++++++++++++++++++
 xen/arch/x86/include/asm/pci.h     | 10 +++++++
 xen/drivers/passthrough/pci.c      |  8 +++---
 4 files changed, 59 insertions(+), 4 deletions(-)
xen/arch/arm/include/asm/pci.h +1 -0
index 80a2431804..8cb46f6b71 100644
@@ -126,6 +126,8 @@ int pci_host_iterate_bridges_and_count(struct domain *d,
   int pci_host_bridge_mappings(struct domain *d);
 +bool pci_check_bar(const struct pci_dev *pdev, mfn_t start, mfn_t end);
+
 #else   /*!CONFIG_HAS_PCI*/
   struct arch_pci_dev { };
xen/arch/arm/pci/pci-host-common.c +0 -0
index 89ef30028e..0eb121666d 100644

xen/arch/arm/pci/pci-host-common.c line 27

Quoted 4 lines
xen/arch/arm/pci/pci-host-common.c +2 -0
@@ -24,6 +24,16 @@
   #include <asm/setup.h>
 +/*
+ * struct to hold pci device bar.
+ */

I find this comment a bit misleading. What you are storing is a candidate region. IOW, this may or may not be a PCI device bar.

Quoted 1 line

Given the current use below, I would rename the structure to something more specific like: pdev_bar_check.

Ack.

Quoted 16 lines
Quoted 16 lines
xen/arch/arm/pci/pci-host-common.c +8 -0
+struct pdev_bar
+{
+    mfn_t start;
+    mfn_t end;
+    bool is_valid;
+};
+
 /*
  * List for all the pci host bridges.
  */
@@ -363,6 +373,39 @@ int __init pci_host_bridge_mappings(struct domain *d)
     return 0;
 }
 +static int is_bar_valid(const struct dt_device_node *dev,
+                        uint64_t addr, uint64_t len, void *data)
Quoted 8 lines

xen/arch/arm/pci/pci-host-common.c int __init pci_host_bridge_mappings(struct domain *d) · line 383

Quoted 6 lines
xen/arch/arm/pci/pci-host-common.c +6 -0
+{
+    struct pdev_bar *bar_data = data;
+    unsigned long s = mfn_x(bar_data->start);
+    unsigned long e = mfn_x(bar_data->end);
+
+    if ( (s <= e) && (s >= PFN_DOWN(addr)) && (e <= PFN_UP(addr + len - 1)) )

AFAICT 's' and 'e' are provided by pci_check_bar() and will never change. So can we move the check 's <= e' outside of the callback?

Yes, We can move the check outside the callback but I feel that if we check here then it is more readable that we are checking for all possible values in one statement. Let me know your view on this.

Quoted 10 lines

xen/arch/arm/pci/pci-host-common.c int __init pci_host_bridge_mappings(struct domain *d) · line 390

Quoted 8 lines
xen/arch/arm/pci/pci-host-common.c +7 -0
+        bar_data->is_valid =  true;
+
+    return 0;
+}
+
+bool pci_check_bar(const struct pci_dev *pdev, mfn_t start, mfn_t end)
+{

Other than the current calls in check_pdev(), do you have plan to use it in more places? The reason I am asking it is this function is non-trivial on Arm (dt_for_each_range() is quite complex).

I don’t see any use of this function in more places. As this function will be called during dom0 boot when the PCI devices are added I don’t see any performance issues. We may need to revisit this function when we add ACPI PCI passthrough support. I will add TODO that we need to revisit this function for ACPI PCI passthrough support.

Regards,
Rahul

#13 lore raw

Re: [PATCH v3 2/2] xen/pci: replace call to is_memory_hole to pci_check_bar

Quoted 76 lines · Rahul Singh

Hi Julien,

Quoted 46 lines · Julien Grall

Hi Rahul,

Quoted 33 lines · Rahul Singh

is_memory_hole was implemented for x86 and not for ARM when introduced. Replace is_memory_hole call to pci_check_bar as function should check if device BAR is in defined memory range. Also, add an implementation for ARM which is required for PCI passthrough.
On x86, pci_check_bar will call is_memory_hole which will check if BAR is not overlapping with any memory region defined in the memory map. On ARM, pci_check_bar will go through the host bridge ranges and check if the BAR is in the range of defined ranges.

---
Changes in v3:
- fix minor comments

---
  xen/arch/arm/include/asm/pci.h     |  2 ++
  xen/arch/arm/pci/pci-host-common.c | 43 ++++++++++++++++++++++++++++++
  xen/arch/x86/include/asm/pci.h     | 10 +++++++
  xen/drivers/passthrough/pci.c      |  8 +++---
  4 files changed, 59 insertions(+), 4 deletions(-)
xen/arch/arm/include/asm/pci.h +1 -0
index 80a2431804..8cb46f6b71 100644
@@ -126,6 +126,8 @@ int pci_host_iterate_bridges_and_count(struct domain *d,
    int pci_host_bridge_mappings(struct domain *d);
  +bool pci_check_bar(const struct pci_dev *pdev, mfn_t start, mfn_t end);
+
  #else   /*!CONFIG_HAS_PCI*/
    struct arch_pci_dev { };
xen/arch/arm/pci/pci-host-common.c +0 -0
index 89ef30028e..0eb121666d 100644

xen/arch/arm/pci/pci-host-common.c line 27

Quoted 4 lines
xen/arch/arm/pci/pci-host-common.c +2 -0
@@ -24,6 +24,16 @@
    #include <asm/setup.h>
  +/*
+ * struct to hold pci device bar.
+ */

I find this comment a bit misleading. What you are storing is a candidate region. IOW, this may or may not be a PCI device bar.

Quoted 1 line

Given the current use below, I would rename the structure to something more specific like: pdev_bar_check.

Ack.

Quoted 16 lines
Quoted 16 lines
xen/arch/arm/pci/pci-host-common.c +8 -0
+struct pdev_bar
+{
+    mfn_t start;
+    mfn_t end;
+    bool is_valid;
+};
+
  /*
   * List for all the pci host bridges.
   */
@@ -363,6 +373,39 @@ int __init pci_host_bridge_mappings(struct domain *d)
      return 0;
  }
  +static int is_bar_valid(const struct dt_device_node *dev,
+                        uint64_t addr, uint64_t len, void *data)
Quoted 8 lines

xen/arch/arm/pci/pci-host-common.c int __init pci_host_bridge_mappings(struct domain *d) · line 383

Quoted 6 lines
xen/arch/arm/pci/pci-host-common.c +6 -0
+{
+    struct pdev_bar *bar_data = data;
+    unsigned long s = mfn_x(bar_data->start);
+    unsigned long e = mfn_x(bar_data->end);
+
+    if ( (s <= e) && (s >= PFN_DOWN(addr)) && (e <= PFN_UP(addr + len - 1)) )

AFAICT 's' and 'e' are provided by pci_check_bar() and will never change. So can we move the check 's <= e' outside of the callback?

Quoted 2 lines

Yes, We can move the check outside the callback but I feel that if we check here then it is more readable that we are checking for all possible values in one statement. Let me know your view on this.

The readability is really a matter of taste here. But my point is more on the number of time a check is done.

It seems pointless to do the same check N times when you know the values are not going to change. Admittedly, the operation is fast (this is a comparison) and N should be small (?).

However, I think it raises the question on where do you draw the line?

Personally, I think all invariant should be checked outside of callbacks. So the line is very clear.

Quoted 12 lines
Quoted 11 lines

xen/arch/arm/pci/pci-host-common.c int __init pci_host_bridge_mappings(struct domain *d) · line 390

Quoted 9 lines
xen/arch/arm/pci/pci-host-common.c +7 -0
+        bar_data->is_valid =  true;
+
+    return 0;
+}
+
+bool pci_check_bar(const struct pci_dev *pdev, mfn_t start, mfn_t end)
+{

Other than the current calls in check_pdev(), do you have plan to use it in more places? The reason I am asking it is this function is non-trivial on Arm (dt_for_each_range() is quite complex).

Quoted 3 lines

I don’t see any use of this function in more places. As this function will be called during dom0 boot when the PCI devices are added I don’t see any performance issues. We may need to revisit this function when we add ACPI PCI passthrough support. I will add TODO that we need to revisit this function for ACPI PCI passthrough support.

Thanks.

Cheers,

Signature · 1 line
Julien Grall
#5 lore raw

Re: [PATCH v3 2/2] xen/pci: replace call to is_memory_hole to pci_check_bar

In reply to Stefano Stabellini
Quoted 29 lines · Stefano Stabellini

xen/arch/arm/pci/pci-host-common.c int __init pci_host_bridge_mappings(struct domain *d) · line 384

Quoted 13 lines · Rahul Singh
xen/arch/arm/pci/pci-host-common.c +9 -0
@@ -363,6 +373,39 @@ int __init pci_host_bridge_mappings(struct domain *d)
     return 0;
 }
 
+static int is_bar_valid(const struct dt_device_node *dev,
+                        uint64_t addr, uint64_t len, void *data)
+{
+    struct pdev_bar *bar_data = data;
+    unsigned long s = mfn_x(bar_data->start);
+    unsigned long e = mfn_x(bar_data->end);
+
+    if ( (s <= e) && (s >= PFN_DOWN(addr)) && (e <= PFN_UP(addr + len - 1)) )
+        bar_data->is_valid =  true;

This patch looks good and you addressed all Jan's comment well. Before I ack it, one question.

I know that you made this change to address Jan's comment but using PFN_DOWN for the (s >= PFN_DOWN(addr)) check and PFN_UP for the (e <= PFN_UP(addr + len - 1)) check means that we are relaxing the requirements, aren't we?

I know that this discussion is a bit pointless because addr and len should always be page aligned, and if they weren't it would be a mistake. But assuming that they are not page aligned, wouldn't we want this check to be a strict as possible?

Quoted 5 lines

Wouldn't we want to ensure that the [s,e] range is a strict subset of [addr,addr+len-1] ? If so we would need to do the following instead:

    if ( (s <= e) && (s >= PFN_UP(addr)) && (e <= PFN_DOWN(addr + len - 1)) )
        bar_data->is_valid =  true;

But that might mean (in theory at least) a partial overlap, which has to be avoided. The only alternative that I see to Rahul's original code is to omit use of PFN_DOWN() and PFN_UP() in this construct altogether. Assuming that's correct for the passed in (addr,len) tuple.

Jan

#14 lore raw

Re: [PATCH v3 2/2] xen/pci: replace call to is_memory_hole to pci_check_bar

In reply to Julien Grall

Hi Julien,

Quoted 80 lines · Julien Grall
Quoted 73 lines · Rahul Singh

Hi Julien,

Quoted 46 lines · Julien Grall

Hi Rahul,

Quoted 33 lines · Rahul Singh

is_memory_hole was implemented for x86 and not for ARM when introduced. Replace is_memory_hole call to pci_check_bar as function should check if device BAR is in defined memory range. Also, add an implementation for ARM which is required for PCI passthrough.
On x86, pci_check_bar will call is_memory_hole which will check if BAR is not overlapping with any memory region defined in the memory map. On ARM, pci_check_bar will go through the host bridge ranges and check if the BAR is in the range of defined ranges.

---
Changes in v3:
- fix minor comments

---
 xen/arch/arm/include/asm/pci.h     |  2 ++
 xen/arch/arm/pci/pci-host-common.c | 43 ++++++++++++++++++++++++++++++
 xen/arch/x86/include/asm/pci.h     | 10 +++++++
 xen/drivers/passthrough/pci.c      |  8 +++---
 4 files changed, 59 insertions(+), 4 deletions(-)
xen/arch/arm/include/asm/pci.h +1 -0
index 80a2431804..8cb46f6b71 100644
@@ -126,6 +126,8 @@ int pci_host_iterate_bridges_and_count(struct domain *d,
   int pci_host_bridge_mappings(struct domain *d);
 +bool pci_check_bar(const struct pci_dev *pdev, mfn_t start, mfn_t end);
+
 #else   /*!CONFIG_HAS_PCI*/
   struct arch_pci_dev { };
xen/arch/arm/pci/pci-host-common.c +0 -0
index 89ef30028e..0eb121666d 100644

xen/arch/arm/pci/pci-host-common.c line 27

Quoted 4 lines
xen/arch/arm/pci/pci-host-common.c +2 -0
@@ -24,6 +24,16 @@
   #include <asm/setup.h>
 +/*
+ * struct to hold pci device bar.
+ */

I find this comment a bit misleading. What you are storing is a candidate region. IOW, this may or may not be a PCI device bar.

Quoted 1 line

Given the current use below, I would rename the structure to something more specific like: pdev_bar_check.

Ack.

Quoted 16 lines
Quoted 16 lines
xen/arch/arm/pci/pci-host-common.c +8 -0
+struct pdev_bar
+{
+    mfn_t start;
+    mfn_t end;
+    bool is_valid;
+};
+
 /*
  * List for all the pci host bridges.
  */
@@ -363,6 +373,39 @@ int __init pci_host_bridge_mappings(struct domain *d)
     return 0;
 }
 +static int is_bar_valid(const struct dt_device_node *dev,
+                        uint64_t addr, uint64_t len, void *data)
Quoted 8 lines

xen/arch/arm/pci/pci-host-common.c int __init pci_host_bridge_mappings(struct domain *d) · line 383

Quoted 6 lines
xen/arch/arm/pci/pci-host-common.c +6 -0
+{
+    struct pdev_bar *bar_data = data;
+    unsigned long s = mfn_x(bar_data->start);
+    unsigned long e = mfn_x(bar_data->end);
+
+    if ( (s <= e) && (s >= PFN_DOWN(addr)) && (e <= PFN_UP(addr + len - 1)) )

AFAICT 's' and 'e' are provided by pci_check_bar() and will never change. So can we move the check 's <= e' outside of the callback?

Quoted 2 lines

Yes, We can move the check outside the callback but I feel that if we check here then it is more readable that we are checking for all possible values in one statement. Let me know your view on this.

Quoted 8 lines

The readability is really a matter of taste here. But my point is more on the number of time a check is done.

It seems pointless to do the same check N times when you know the values are not going to change. Admittedly, the operation is fast (this is a comparison) and N should be small (?).

However, I think it raises the question on where do you draw the line?

Personally, I think all invariant should be checked outside of callbacks. So the line is very clear.

I will move the check for "s <=e” outside the callback and will send it for review.

Regards,
Rahul

#6 lore raw

Re: [PATCH v3 2/2] xen/pci: replace call to is_memory_hole to pci_check_bar

In reply to Jan Beulich

Hi Jan & Stefano,

Quoted 20 lines · Jan Beulich
Quoted 19 lines · Stefano Stabellini

xen/arch/arm/pci/pci-host-common.c int __init pci_host_bridge_mappings(struct domain *d) · line 384

Quoted 13 lines · Rahul Singh
xen/arch/arm/pci/pci-host-common.c +9 -0
@@ -363,6 +373,39 @@ int __init pci_host_bridge_mappings(struct domain *d)
      return 0;
  }
 
+static int is_bar_valid(const struct dt_device_node *dev,
+                        uint64_t addr, uint64_t len, void *data)
+{
+    struct pdev_bar *bar_data = data;
+    unsigned long s = mfn_x(bar_data->start);
+    unsigned long e = mfn_x(bar_data->end);
+
+    if ( (s <= e) && (s >= PFN_DOWN(addr)) && (e <= PFN_UP(addr + len - 1)) )
+        bar_data->is_valid =  true;

This patch looks good and you addressed all Jan's comment well. Before I ack it, one question.

Quoted 7 lines
Quoted 7 lines

I know that you made this change to address Jan's comment but using PFN_DOWN for the (s >= PFN_DOWN(addr)) check and PFN_UP for the (e <= PFN_UP(addr + len - 1)) check means that we are relaxing the requirements, aren't we?

I know that this discussion is a bit pointless because addr and len should always be page aligned, and if they weren't it would be a mistake.

Hmmm.... Is that requirement written down somewhere? The reason I am asking is "page-aligned" will vary depending on the software. In the past we had a couple of cases where the region would be 4KB-aligned but not necessarily 64KB-aligned.

If the answer is no to my question then...

Quoted 7 lines

But

Quoted 6 lines

assuming that they are not page aligned, wouldn't we want this check to be a strict as possible?

Wouldn't we want to ensure that the [s,e] range is a strict subset of [addr,addr+len-1] ? If so we would need to do the following instead:

Quoted 8 lines
Quoted 2 lines
     if ( (s <= e) && (s >= PFN_UP(addr)) && (e <= PFN_DOWN(addr + len - 1)) )
         bar_data->is_valid =  true;

But that might mean (in theory at least) a partial overlap, which has to be avoided. The only alternative that I see to Rahul's original code is to omit use of PFN_DOWN() and PFN_UP() in this construct altogether. Assuming that's correct for the passed in (addr,len) tuple.

... I think we would want to drop PFN_DOWN/PFN_UP here.

Cheers,

Signature · 1 line
Julien Grall
#7 lore raw

Re: [PATCH v3 2/2] xen/pci: replace call to is_memory_hole to pci_check_bar

Quoted 28 lines · Julien Grall

Hi Jan & Stefano,

Quoted 25 lines · Jan Beulich
Quoted 24 lines · Stefano Stabellini

xen/arch/arm/pci/pci-host-common.c int __init pci_host_bridge_mappings(struct domain *d) · line 384

Quoted 13 lines · Rahul Singh
xen/arch/arm/pci/pci-host-common.c +9 -0
@@ -363,6 +373,39 @@ int __init pci_host_bridge_mappings(struct domain *d)
      return 0;
  }
 
+static int is_bar_valid(const struct dt_device_node *dev,
+                        uint64_t addr, uint64_t len, void *data)
+{
+    struct pdev_bar *bar_data = data;
+    unsigned long s = mfn_x(bar_data->start);
+    unsigned long e = mfn_x(bar_data->end);
+
+    if ( (s <= e) && (s >= PFN_DOWN(addr)) && (e <= PFN_UP(addr + len - 1)) )
+        bar_data->is_valid =  true;

This patch looks good and you addressed all Jan's comment well. Before I ack it, one question.

I know that you made this change to address Jan's comment but using PFN_DOWN for the (s >= PFN_DOWN(addr)) check and PFN_UP for the (e <= PFN_UP(addr + len - 1)) check means that we are relaxing the requirements, aren't we?

Quoted 4 lines
Quoted 2 lines
Quoted 2 lines

I know that this discussion is a bit pointless because addr and len should always be page aligned, and if they weren't it would be a mistake.

Hmmm.... Is that requirement written down somewhere?

What do you mean here? Isn't it quite obvious that every byte in the address space may only be used for a single purpose? I.e. if a byte is covered by a BAR, it cannot also be covered by a RAM region or yet something else (e.g. MMIO beyond BARs of PCI devices). What happens if BAR and RAM indeed overlap depends on fabric and chipset, but it'll either result in chaos if two parties respond to a single request on the bus, or it'll be (hopefully) deterministic (for any individual system) which of the two takes "precedence".

I think we've had a similar discussion a little while ago already in the context of vPCI with guest address space in mind. The same (imo obvious) "rule" spelled out above applies there and here.

Jan

Quoted 27 lines

The reason I am
asking is "page-aligned" will vary depending on the software. In the past we had a couple of cases where the region would be 4KB-aligned but not necessarily 64KB-aligned.

If the answer is no to my question then...

Quoted 7 lines

But

Quoted 6 lines

assuming that they are not page aligned, wouldn't we want this check to be a strict as possible?

Wouldn't we want to ensure that the [s,e] range is a strict subset of [addr,addr+len-1] ? If so we would need to do the following instead:

Quoted 8 lines
Quoted 2 lines
     if ( (s <= e) && (s >= PFN_UP(addr)) && (e <= PFN_DOWN(addr + len - 1)) )
         bar_data->is_valid =  true;

But that might mean (in theory at least) a partial overlap, which has to be avoided. The only alternative that I see to Rahul's original code is to omit use of PFN_DOWN() and PFN_UP() in this construct altogether. Assuming that's correct for the passed in (addr,len) tuple.

... I think we would want to drop PFN_DOWN/PFN_UP here.

Cheers,

#8 lore raw

Re: [PATCH v3 2/2] xen/pci: replace call to is_memory_hole to pci_check_bar

Hi Jan,

Quoted 34 lines · Jan Beulich
Quoted 28 lines · Julien Grall

Hi Jan & Stefano,

Quoted 25 lines · Jan Beulich
Quoted 24 lines · Stefano Stabellini

xen/arch/arm/pci/pci-host-common.c int __init pci_host_bridge_mappings(struct domain *d) · line 384

Quoted 13 lines · Rahul Singh
xen/arch/arm/pci/pci-host-common.c +9 -0
@@ -363,6 +373,39 @@ int __init pci_host_bridge_mappings(struct domain *d)
       return 0;
   }
 
+static int is_bar_valid(const struct dt_device_node *dev,
+                        uint64_t addr, uint64_t len, void *data)
+{
+    struct pdev_bar *bar_data = data;
+    unsigned long s = mfn_x(bar_data->start);
+    unsigned long e = mfn_x(bar_data->end);
+
+    if ( (s <= e) && (s >= PFN_DOWN(addr)) && (e <= PFN_UP(addr + len - 1)) )
+        bar_data->is_valid =  true;

This patch looks good and you addressed all Jan's comment well. Before I ack it, one question.

I know that you made this change to address Jan's comment but using PFN_DOWN for the (s >= PFN_DOWN(addr)) check and PFN_UP for the (e <= PFN_UP(addr + len - 1)) check means that we are relaxing the requirements, aren't we?

Quoted 4 lines
Quoted 2 lines
Quoted 2 lines

I know that this discussion is a bit pointless because addr and len should always be page aligned, and if they weren't it would be a mistake.

Hmmm.... Is that requirement written down somewhere?

Quoted 8 lines

What do you mean here? Isn't it quite obvious that every byte in the address space may only be used for a single purpose? I.e. if a byte is covered by a BAR, it cannot also be covered by a RAM region or yet something else (e.g. MMIO beyond BARs of PCI devices). What happens if BAR and RAM indeed overlap depends on fabric and chipset, but it'll either result in chaos if two parties respond to a single request on the bus, or it'll be (hopefully) deterministic (for any individual system) which of the two takes "precedence".

I am well aware about that and I am not sure how you implied this is what I was referring to from what I wrote (in particular if you read the next sentence).

Stefano wrote that it would be a mistake if the address/length is not page-aligned. However, I am not aware from such requirement written down. It seems to be more an expected common sense that was IIRC not always respected on HW supporting multiple page-granularity.

Cheers,

Signature · 1 line
Julien Grall
#9 lore raw

Re: [PATCH v3 2/2] xen/pci: replace call to is_memory_hole to pci_check_bar

Quoted 42 lines · Julien Grall
Quoted 32 lines · Jan Beulich
Quoted 26 lines · Julien Grall
Quoted 25 lines · Jan Beulich
Quoted 24 lines · Stefano Stabellini

xen/arch/arm/pci/pci-host-common.c int __init pci_host_bridge_mappings(struct domain *d) · line 384

Quoted 13 lines · Rahul Singh
xen/arch/arm/pci/pci-host-common.c +9 -0
@@ -363,6 +373,39 @@ int __init pci_host_bridge_mappings(struct domain *d)
       return 0;
   }
 
+static int is_bar_valid(const struct dt_device_node *dev,
+                        uint64_t addr, uint64_t len, void *data)
+{
+    struct pdev_bar *bar_data = data;
+    unsigned long s = mfn_x(bar_data->start);
+    unsigned long e = mfn_x(bar_data->end);
+
+    if ( (s <= e) && (s >= PFN_DOWN(addr)) && (e <= PFN_UP(addr + len - 1)) )
+        bar_data->is_valid =  true;

This patch looks good and you addressed all Jan's comment well. Before I ack it, one question.

I know that you made this change to address Jan's comment but using PFN_DOWN for the (s >= PFN_DOWN(addr)) check and PFN_UP for the (e <= PFN_UP(addr + len - 1)) check means that we are relaxing the requirements, aren't we?

Quoted 4 lines
Quoted 2 lines
Quoted 2 lines

I know that this discussion is a bit pointless because addr and len should always be page aligned, and if they weren't it would be a mistake.

Hmmm.... Is that requirement written down somewhere?

Quoted 8 lines

What do you mean here? Isn't it quite obvious that every byte in the address space may only be used for a single purpose? I.e. if a byte is covered by a BAR, it cannot also be covered by a RAM region or yet something else (e.g. MMIO beyond BARs of PCI devices). What happens if BAR and RAM indeed overlap depends on fabric and chipset, but it'll either result in chaos if two parties respond to a single request on the bus, or it'll be (hopefully) deterministic (for any individual system) which of the two takes "precedence".

Quoted 8 lines

I am well aware about that and I am not sure how you implied this is what I was referring to from what I wrote (in particular if you read the next sentence).

Stefano wrote that it would be a mistake if the address/length is not page-aligned. However, I am not aware from such requirement written down. It seems to be more an expected common sense that was IIRC not always respected on HW supporting multiple page-granularity.

I guess the question was then solely directed at Stefano? I have no idea, after all, what guarantees there are for addr and len, which presumably originate from DT somewhere.

Yet if there aren't any guarantees, then aligning the incoming range may be necessary: BARs can be sub-page size (even more so when considering page sizes above 4k), and so a not-really-overlap (at byte granularity) could still be an issue if there's an overlap at 4k or system page size granularity.

Jan

#10 lore raw

Re: [PATCH v3 2/2] xen/pci: replace call to is_memory_hole to pci_check_bar

Quoted 47 lines · Jan Beulich
Quoted 46 lines · Julien Grall
Quoted 32 lines · Jan Beulich
Quoted 26 lines · Julien Grall
Quoted 25 lines · Jan Beulich
Quoted 24 lines · Stefano Stabellini

Review int __init pci_host_bridge_mappings(struct domain *d) · line 384

Quoted 13 lines · Rahul Singh
(quoted diff) +9 -0
@@ -363,6 +373,39 @@ int __init pci_host_bridge_mappings(struct domain *d)
        return 0;
    }
 
+static int is_bar_valid(const struct dt_device_node *dev,
+                        uint64_t addr, uint64_t len, void *data)
+{
+    struct pdev_bar *bar_data = data;
+    unsigned long s = mfn_x(bar_data->start);
+    unsigned long e = mfn_x(bar_data->end);
+
+    if ( (s <= e) && (s >= PFN_DOWN(addr)) && (e <= PFN_UP(addr + len - 1)) )
+        bar_data->is_valid =  true;

This patch looks good and you addressed all Jan's comment well. Before I ack it, one question.

I know that you made this change to address Jan's comment but using PFN_DOWN for the (s >= PFN_DOWN(addr)) check and PFN_UP for the (e <= PFN_UP(addr + len - 1)) check means that we are relaxing the requirements, aren't we?

Quoted 4 lines
Quoted 2 lines
Quoted 2 lines

I know that this discussion is a bit pointless because addr and len should always be page aligned, and if they weren't it would be a mistake.

Hmmm.... Is that requirement written down somewhere?

Quoted 8 lines

What do you mean here? Isn't it quite obvious that every byte in the address space may only be used for a single purpose? I.e. if a byte is covered by a BAR, it cannot also be covered by a RAM region or yet something else (e.g. MMIO beyond BARs of PCI devices). What happens if BAR and RAM indeed overlap depends on fabric and chipset, but it'll either result in chaos if two parties respond to a single request on the bus, or it'll be (hopefully) deterministic (for any individual system) which of the two takes "precedence".

I am well aware about that and I am not sure how you implied this is what I was referring to from what I wrote (in particular if you read the next sentence).

Quoted 6 lines
Quoted 4 lines

Stefano wrote that it would be a mistake if the address/length is not page-aligned. However, I am not aware from such requirement written down. It seems to be more an expected common sense that was IIRC not always respected on HW supporting multiple page-granularity.

I guess the question was then solely directed at Stefano?

This question yes. The rest was a reply to your suggestion. I will wait for Stefano to answer.

Cheers,

Signature · 1 line
Julien Grall