git: fcc350c375f7 - main - LinuxKPI: Add pci_iomap_range function

From: Vladimir Kondratyev <wulf_at_FreeBSD.org>
Date: Sun, 21 Jul 2024 13:14:57 UTC
The branch main has been updated by wulf:

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

commit fcc350c375f776318d0da8021109631492ab9261
Author:     Vladimir Kondratyev <wulf@FreeBSD.org>
AuthorDate: 2024-07-21 13:10:25 +0000
Commit:     Vladimir Kondratyev <wulf@FreeBSD.org>
CommitDate: 2024-07-21 13:10:25 +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
---
 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 9c6a52aab83f..9457599d31d3 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 51a3b0b2ebe1..3287a8a15ff5 100644
--- a/sys/compat/linuxkpi/common/src/linux_pci.c
+++ b/sys/compat/linuxkpi/common/src/linux_pci.c
@@ -755,7 +755,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;
 
@@ -765,7 +766,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
@@ -774,7 +782,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);