[PATCH v3 2/2] PCI/P2PDMA: Modify p2p_dma_distance to detect P2P links

Shivasharan S
2024-12-11T07:29:42Z

Update the p2p_dma_distance() to determine inter-switch P2P links existing
between two switches and use this to calculate the DMA distance between
two devices. This requires enabling the PCIE_P2P_LINK config option in
the kernel.

Signed-off-by: Shivasharan S <shivasharan.srikanteshwara@broadcom.com>
---
Changes in v3:
Fixed review comments from Jonathan

drivers/pci/p2pdma.c | 18 +++++++++++++++++-
drivers/pci/pcie/p2p_link.c | 18 ++++++++++++++++++
drivers/pci/pcie/p2p_link.h | 5 +++++
3 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/drivers/pci/p2pdma.c b/drivers/pci/p2pdma.c
index 7abd4f546d3c..9482bf0b1a02 100644
--- a/drivers/pci/p2pdma.c
+++ b/drivers/pci/p2pdma.c
@@ -20,6 +20,7 @@
#include <linux/random.h>
#include <linux/seq_buf.h>
#include <linux/xarray.h>
+#include "pcie/p2p_link.h"

struct pci_p2pdma {
struct gen_pool *pool;
@@ -576,7 +577,7 @@ calc_map_type_and_dist(struct pci_dev *provider, struct pci_dev *client,
int *dist, bool verbose)
{
enum pci_p2pdma_map_type map_type = PCI_P2PDMA_MAP_THRU_HOST_BRIDGE;
- struct pci_dev *a = provider, *b = client, *bb;
+ struct pci_dev *a = provider, *b = client, *bb, *b_p2p_link = NULL;
bool acs_redirects = false;
struct pci_p2pdma *p2pdma;
struct seq_buf acs_list;
@@ -606,6 +607,18 @@ calc_map_type_and_dist(struct pci_dev *provider, struct pci_dev *client,
if (a == bb)
goto check_b_path_acs;

+#ifdef CONFIG_PCIE_P2P_LINK
+ /*
+ * If both upstream bridges have Inter switch P2P link
+ * available, P2P DMA distance can account for optimized
+ * path.
+ */
+ if (pcie_port_is_p2p_link_available(a, bb)) {
+ b_p2p_link = bb;
+ goto check_b_path_acs;
+ }
+#endif
+
bb = pci_upstream_bridge(bb);
dist_b++;
}
@@ -629,6 +642,9 @@ calc_map_type_and_dist(struct pci_dev *provider, struct pci_dev *client,
acs_cnt++;
}

+ if (bb == b_p2p_link)
+ break;
+
bb = pci_upstream_bridge(bb);
}

diff --git a/drivers/pci/pcie/p2p_link.c b/drivers/pci/pcie/p2p_link.c
index dec5c4cbcf13..87651dfa1981 100644
--- a/drivers/pci/pcie/p2p_link.c
+++ b/drivers/pci/pcie/p2p_link.c
@@ -141,3 +141,21 @@ void p2p_link_sysfs_update_group(struct pci_dev *pdev)

sysfs_update_group(&pdev->dev.kobj, &pcie_port_p2p_link_attr_group);
}
+
+/*
+ * pcie_port_is_p2p_link_available: Determine if a P2P link is available
+ * between the two upstream bridges. The serial number of the two devices
+ * will be compared and if they are same then it is considered that the P2P
+ * link is available.
+ *
+ * Return value: true if inter switch P2P is available, return false otherwise.
+ */
+bool pcie_port_is_p2p_link_available(struct pci_dev *a, struct pci_dev *b)
+{
+ if (!pcie_port_is_p2p_supported(a) || !pcie_port_is_p2p_supported(b))
+ return false;
+
+ /* the above check validates DSN is valid for both devices */
+ return pci_get_dsn(a) == pci_get_dsn(b);
+}
+EXPORT_SYMBOL_GPL(pcie_port_is_p2p_link_available);
diff --git a/drivers/pci/pcie/p2p_link.h b/drivers/pci/pcie/p2p_link.h
index 6c4f57841c79..6677ed66f397 100644
--- a/drivers/pci/pcie/p2p_link.h
+++ b/drivers/pci/pcie/p2p_link.h
@@ -21,7 +21,12 @@
#ifdef CONFIG_PCIE_P2P_LINK
void p2p_link_sysfs_update_group(struct pci_dev *pdev);

+bool pcie_port_is_p2p_link_available(struct pci_dev *a, struct pci_dev *b);
#else
static inline void p2p_link_sysfs_update_group(struct pci_dev *pdev) { }
+static inline bool pcie_port_is_p2p_link_available(struct pci_dev *a, struct pci_dev *b)
+{
+ return false;
+}
#endif
#endif /* _P2P_LINK_H_ */
--
2.43.0