The P2PDMA code currently provides two features under the same
CONFIG_PCI_P2PDMA option:

 1.  Locate providers via pcim_p2pdma_provider()
 2.  Manage actual P2P DMA

Other code (such as vfio-pci) depends on 1, without having a hard
dependency on 2.

A future commit expands the use of DMABUF in vfio-pci for non-P2P
scenarios, relying on pcim_p2pdma_provider() always being present.  If
that depended on CONFIG_PCI_P2PDMA, it would make vfio-pci only
available if CONFIG_ZONE_DEVICE is present (e.g. 64-bit systems), even
when P2P is not needed.

To resolve this, introduce CONFIG_PCI_P2PDMA_CORE which contains the
basic provider functionality to make it available even if the
CONFIG_PCI_P2PDMA feature is disabled or unavailable due to
!CONFIG_ZONE_DEVICE.  Users such as vfio-pci can enable their own P2P
features based off the original CONFIG_PCI_P2PDMA (available when
CONFIG_ZONE_DEVICE is set).

Signed-off-by: Matt Evans <[email protected]>
---
 drivers/pci/Kconfig        | 10 +++++-----
 drivers/pci/Makefile       |  2 +-
 drivers/pci/p2pdma.c       | 16 ++++++++++++++++
 include/linux/pci-p2pdma.h | 24 ++++++++++++++----------
 include/linux/pci.h        |  2 +-
 5 files changed, 37 insertions(+), 17 deletions(-)

diff --git a/drivers/pci/Kconfig b/drivers/pci/Kconfig
index 33c88432b728..59d70bc84cc9 100644
--- a/drivers/pci/Kconfig
+++ b/drivers/pci/Kconfig
@@ -206,11 +206,7 @@ config PCIE_TPH
 config PCI_P2PDMA
        bool "PCI peer-to-peer transfer support"
        depends on ZONE_DEVICE
-       #
-       # The need for the scatterlist DMA bus address flag means PCI P2PDMA
-       # requires 64bit
-       #
-       depends on 64BIT
+       select PCI_P2PDMA_CORE
        select GENERIC_ALLOCATOR
        select NEED_SG_DMA_FLAGS
        help
@@ -226,6 +222,10 @@ config PCI_P2PDMA
 
          If unsure, say N.
 
+config PCI_P2PDMA_CORE
+       default n
+       bool
+
 config PCI_LABEL
        def_bool y if (DMI || ACPI)
        select NLS
diff --git a/drivers/pci/Makefile b/drivers/pci/Makefile
index 41ebc3b9a518..419b646a301d 100644
--- a/drivers/pci/Makefile
+++ b/drivers/pci/Makefile
@@ -30,7 +30,7 @@ obj-$(CONFIG_PCI_SYSCALL)     += syscall.o
 obj-$(CONFIG_PCI_STUB)         += pci-stub.o
 obj-$(CONFIG_PCI_PF_STUB)      += pci-pf-stub.o
 obj-$(CONFIG_PCI_ECAM)         += ecam.o
-obj-$(CONFIG_PCI_P2PDMA)       += p2pdma.o
+obj-$(CONFIG_PCI_P2PDMA_CORE)  += p2pdma.o
 obj-$(CONFIG_XEN_PCIDEV_FRONTEND) += xen-pcifront.o
 obj-$(CONFIG_VGA_ARB)          += vgaarb.o
 obj-$(CONFIG_PCI_DOE)          += doe.o
diff --git a/drivers/pci/p2pdma.c b/drivers/pci/p2pdma.c
index 7c898542af8d..619d46c652b8 100644
--- a/drivers/pci/p2pdma.c
+++ b/drivers/pci/p2pdma.c
@@ -28,6 +28,14 @@ struct pci_p2pdma {
        struct p2pdma_provider mem[PCI_STD_NUM_BARS];
 };
 
+/*
+ * CONFIG_PCI_P2PDMA_CORE provides just a bare-bones init and
+ * pcim_p2pdma_provider() interface (used by things like VFIO even if
+ * full P2PDMA isn't present).  The full P2PDMA feature is under the
+ * CONFIG_PCI_P2PDMA option.
+ */
+#ifdef CONFIG_PCI_P2PDMA
+
 struct pci_p2pdma_pagemap {
        struct dev_pagemap pgmap;
        struct p2pdma_provider *mem;
@@ -226,6 +234,8 @@ static const struct dev_pagemap_ops p2pdma_pgmap_ops = {
        .folio_free = p2pdma_folio_free,
 };
 
+#endif /* CONFIG_PCI_P2PDMA */
+
 static void pci_p2pdma_release(void *data)
 {
        struct pci_dev *pdev = data;
@@ -241,11 +251,13 @@ static void pci_p2pdma_release(void *data)
                synchronize_rcu();
        xa_destroy(&p2pdma->map_types);
 
+#ifdef CONFIG_PCI_P2PDMA
        if (!p2pdma->pool)
                return;
 
        gen_pool_destroy(p2pdma->pool);
        sysfs_remove_group(&pdev->dev.kobj, &p2pmem_group);
+#endif
 }
 
 /**
@@ -330,6 +342,8 @@ struct p2pdma_provider *pcim_p2pdma_provider(struct pci_dev 
*pdev, int bar)
 }
 EXPORT_SYMBOL_GPL(pcim_p2pdma_provider);
 
+#ifdef CONFIG_PCI_P2PDMA
+
 static int pci_p2pdma_setup_pool(struct pci_dev *pdev)
 {
        struct pci_p2pdma *p2pdma;
@@ -1207,3 +1221,5 @@ ssize_t pci_p2pdma_enable_show(char *page, struct pci_dev 
*p2p_dev,
        return sprintf(page, "%s\n", pci_name(p2p_dev));
 }
 EXPORT_SYMBOL_GPL(pci_p2pdma_enable_show);
+
+#endif
diff --git a/include/linux/pci-p2pdma.h b/include/linux/pci-p2pdma.h
index 873de20a2247..4c42a7b2ee85 100644
--- a/include/linux/pci-p2pdma.h
+++ b/include/linux/pci-p2pdma.h
@@ -67,9 +67,22 @@ enum pci_p2pdma_map_type {
        PCI_P2PDMA_MAP_THRU_HOST_BRIDGE,
 };
 
-#ifdef CONFIG_PCI_P2PDMA
+#ifdef CONFIG_PCI_P2PDMA_CORE
 int pcim_p2pdma_init(struct pci_dev *pdev);
 struct p2pdma_provider *pcim_p2pdma_provider(struct pci_dev *pdev, int bar);
+#else
+static inline int pcim_p2pdma_init(struct pci_dev *pdev)
+{
+       return -EOPNOTSUPP;
+}
+static inline struct p2pdma_provider *pcim_p2pdma_provider(struct pci_dev 
*pdev,
+                                                          int bar)
+{
+       return NULL;
+}
+#endif
+
+#ifdef CONFIG_PCI_P2PDMA
 int pci_p2pdma_add_resource(struct pci_dev *pdev, int bar, size_t size,
                u64 offset);
 int pci_p2pdma_distance_many(struct pci_dev *provider, struct device **clients,
@@ -89,15 +102,6 @@ ssize_t pci_p2pdma_enable_show(char *page, struct pci_dev 
*p2p_dev,
 enum pci_p2pdma_map_type pci_p2pdma_map_type(struct p2pdma_provider *provider,
                                             struct device *dev);
 #else /* CONFIG_PCI_P2PDMA */
-static inline int pcim_p2pdma_init(struct pci_dev *pdev)
-{
-       return -EOPNOTSUPP;
-}
-static inline struct p2pdma_provider *pcim_p2pdma_provider(struct pci_dev 
*pdev,
-                                                          int bar)
-{
-       return NULL;
-}
 static inline int pci_p2pdma_add_resource(struct pci_dev *pdev, int bar,
                size_t size, u64 offset)
 {
diff --git a/include/linux/pci.h b/include/linux/pci.h
index 2c4454583c11..531aec355686 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -557,7 +557,7 @@ struct pci_dev {
        u16             pasid_cap;      /* PASID Capability offset */
        u16             pasid_features;
 #endif
-#ifdef CONFIG_PCI_P2PDMA
+#ifdef CONFIG_PCI_P2PDMA_CORE
        struct pci_p2pdma __rcu *p2pdma;
 #endif
 #ifdef CONFIG_PCI_DOE
-- 
2.47.3

Reply via email to