git: 7403f2c657dc - stable/14 - LinuxKPI: Add pci_iomap_range function

From: Vladimir Kondratyev <wulf_at_FreeBSD.org>
Date: Thu, 01 Aug 2024 22:28:06 UTC
The branch stable/14 has been updated by wulf:

URL: https://cgit.FreeBSD.org/src/commit/?id=7403f2c657dc66665d6cff9ab4cd67d2f3d0cf56

commit 7403f2c657dc66665d6cff9ab4cd67d2f3d0cf56
Author:     Vladimir Kondratyev <wulf@FreeBSD.org>
AuthorDate: 2024-07-21 13:10:25 +0000
Commit:     Vladimir Kondratyev <wulf@FreeBSD.org>
CommitDate: 2024-08-01 21:16:23 +0000

    LinuxKPI: Add pci_iomap_range function
    
    pci_iomap_range creates a virtual mapping cookie for a PCI BAR.
    As compared with pci_iomap it got extra offset parameter.
    
    Sponsored by:   Serenity CyberSecurity, LLC
    MFC after:      1 week
    Reviewed by:    manu, bz
    Differential Revision:  https://reviews.freebsd.org/D45904
    
    (cherry picked from commit fcc350c375f776318d0da8021109631492ab9261)
---
 sys/compat/linuxkpi/common/include/linux/pci.h |  4 ++++
 sys/compat/linuxkpi/common/src/linux_pci.c     | 16 +++++++++++++---
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/sys/compat/linuxkpi/common/include/linux/pci.h b/sys/compat/linuxkpi/common/include/linux/pci.h
index 027dc0dfca0b..f9b60ae186b5 100644
--- a/sys/compat/linuxkpi/common/include/linux/pci.h
+++ b/sys/compat/linuxkpi/common/include/linux/pci.h
@@ -362,6 +362,8 @@ bool pci_device_is_present(struct pci_dev *pdev);
 
 int linuxkpi_pcim_enable_device(struct pci_dev *pdev);
 void __iomem **linuxkpi_pcim_iomap_table(struct pci_dev *pdev);
+void *linuxkpi_pci_iomap_range(struct pci_dev *pdev, int mmio_bar,
+    unsigned long mmio_off, unsigned long mmio_size);
 void *linuxkpi_pci_iomap(struct pci_dev *pdev, int mmio_bar, int mmio_size);
 void linuxkpi_pci_iounmap(struct pci_dev *pdev, void *res);
 int linuxkpi_pcim_iomap_regions(struct pci_dev *pdev, uint32_t mask,
@@ -774,6 +776,8 @@ static inline void pci_disable_sriov(struct pci_dev *dev)
 {
 }
 
+#define	pci_iomap_range(pdev, mmio_bar, mmio_off, mmio_size) \
+	linuxkpi_pci_iomap_range(pdev, mmio_bar, mmio_off, mmio_size)
 #define	pci_iomap(pdev, mmio_bar, mmio_size) \
 	linuxkpi_pci_iomap(pdev, mmio_bar, mmio_size)
 #define	pci_iounmap(pdev, res)	linuxkpi_pci_iounmap(pdev, res)
diff --git a/sys/compat/linuxkpi/common/src/linux_pci.c b/sys/compat/linuxkpi/common/src/linux_pci.c
index 3ed0f268d5f9..c41a55fc9f87 100644
--- a/sys/compat/linuxkpi/common/src/linux_pci.c
+++ b/sys/compat/linuxkpi/common/src/linux_pci.c
@@ -756,7 +756,8 @@ _lkpi_pci_iomap(struct pci_dev *pdev, int bar, int mmio_size __unused)
 }
 
 void *
-linuxkpi_pci_iomap(struct pci_dev *pdev, int mmio_bar, int mmio_size)
+linuxkpi_pci_iomap_range(struct pci_dev *pdev, int mmio_bar,
+    unsigned long mmio_off, unsigned long mmio_size)
 {
 	struct resource *res;
 
@@ -766,7 +767,14 @@ linuxkpi_pci_iomap(struct pci_dev *pdev, int mmio_bar, int mmio_size)
 	/* This is a FreeBSD extension so we can use bus_*(). */
 	if (pdev->want_iomap_res)
 		return (res);
-	return ((void *)rman_get_bushandle(res));
+	MPASS(mmio_off < rman_get_size(res));
+	return ((void *)(rman_get_bushandle(res) + mmio_off));
+}
+
+void *
+linuxkpi_pci_iomap(struct pci_dev *pdev, int mmio_bar, int mmio_size)
+{
+	return (linuxkpi_pci_iomap_range(pdev, mmio_bar, 0, mmio_size));
 }
 
 void
@@ -775,7 +783,9 @@ linuxkpi_pci_iounmap(struct pci_dev *pdev, void *res)
 	struct pci_mmio_region *mmio, *p;
 
 	TAILQ_FOREACH_SAFE(mmio, &pdev->mmio, next, p) {
-		if (res != (void *)rman_get_bushandle(mmio->res))
+		if ((bus_space_handle_t)res < rman_get_bushandle(mmio->res) ||
+		    (bus_space_handle_t)res >= rman_get_bushandle(mmio->res) +
+					       rman_get_size(mmio->res))
 			continue;
 		bus_release_resource(pdev->dev.bsddev,
 		    mmio->type, mmio->rid, mmio->res);