git: 4b56afaf7bf4 - main - LinuxKPI: implement irq_get_msi_desc()

From: Bjoern A. Zeeb <bz_at_FreeBSD.org>
Date: Fri, 13 Jan 2023 00:44:36 UTC
The branch main has been updated by bz:

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

commit 4b56afaf7bf4fa37bae5b26fd93ee1ff5969c1bb
Author:     Bjoern A. Zeeb <bz@FreeBSD.org>
AuthorDate: 2022-11-28 18:27:03 +0000
Commit:     Bjoern A. Zeeb <bz@FreeBSD.org>
CommitDate: 2023-01-13 00:43:23 +0000

    LinuxKPI: implement irq_get_msi_desc()
    
    Add irq_get_msi_desc() as a wrapper around a PCI function which will
    allocate a single cached value (see comment on struct) for the
    msi_desc requested if it doesn't exist yet and handle freeing it
    when the PCI device goes away.  We take the values from the ivars of
    the native (FreeBSD) device.
    
    While changing struct pci_dev also add the msi_cap field requested by
    a wireless driver.
    
    Bump __FreeBSD_version so these changes can be detected.
    
    MFC after:      3 days
    X-MFC: move fields to end of struct (alloc happens in linux_pci.c)
    Reviewed by:    hselasky (earlier version)
    Differential Revision: https://reviews.freebsd.org/D37523
---
 UPDATING                                           |  4 +++
 .../linuxkpi/common/include/linux/interrupt.h      |  7 +++++
 sys/compat/linuxkpi/common/include/linux/pci.h     | 18 +++++++++++++
 sys/compat/linuxkpi/common/src/linux_pci.c         | 31 ++++++++++++++++++++++
 sys/sys/param.h                                    |  2 +-
 5 files changed, 61 insertions(+), 1 deletion(-)

diff --git a/UPDATING b/UPDATING
index 13007feb15c6..a0fdb839f668 100644
--- a/UPDATING
+++ b/UPDATING
@@ -27,6 +27,10 @@ NOTE TO PEOPLE WHO THINK THAT FreeBSD 14.x IS SLOW:
 	world, or to merely disable the most expensive debugging functionality
 	at runtime, run "ln -s 'abort:false,junk:false' /etc/malloc.conf".)
 
+20230113:
+	LinuxKPI pci.h changes may require out-of-tree drivers to be recompiled.
+	Bump _FreeBSD_version to 1400078 to be able to detect this change.
+
 20221212:
 	llvm-objump is now always installed as objdump.  Previously there was
 	no /usr/bin/objdump unless the WITH_LLVM_BINUTILS knob was used.
diff --git a/sys/compat/linuxkpi/common/include/linux/interrupt.h b/sys/compat/linuxkpi/common/include/linux/interrupt.h
index 4c914a7b45a1..f56140632605 100644
--- a/sys/compat/linuxkpi/common/include/linux/interrupt.h
+++ b/sys/compat/linuxkpi/common/include/linux/interrupt.h
@@ -131,6 +131,13 @@ irq_set_affinity_hint(int vector, cpumask_t *mask)
 	return (-error);
 }
 
+static inline struct msi_desc *
+irq_get_msi_desc(unsigned int irq)
+{
+
+	return (lkpi_pci_msi_desc_alloc(irq));
+}
+
 /*
  * LinuxKPI tasklet support
  */
diff --git a/sys/compat/linuxkpi/common/include/linux/pci.h b/sys/compat/linuxkpi/common/include/linux/pci.h
index cc9cb7da0b2a..6651fd62b44b 100644
--- a/sys/compat/linuxkpi/common/include/linux/pci.h
+++ b/sys/compat/linuxkpi/common/include/linux/pci.h
@@ -285,6 +285,17 @@ _pci_exit(void)								\
 module_init(_pci_init);							\
 module_exit(_pci_exit)
 
+struct msi_msg {
+	uint32_t			data;
+};
+
+struct msi_desc {
+	struct msi_msg			msg;
+	struct {
+		bool			is_64;
+	} msi_attrib;
+};
+
 /*
  * If we find drivers accessing this from multiple KPIs we may have to
  * refcount objects of this structure.
@@ -311,12 +322,18 @@ struct pci_dev {
 	unsigned int		devfn;
 	uint32_t		class;
 	uint8_t			revision;
+	uint8_t			msi_cap;
 	bool			managed;	/* devres "pcim_*(). */
 	bool			want_iomap_res;
 	bool			msi_enabled;
 	bool			msix_enabled;
 	phys_addr_t		rom;
 	size_t			romlen;
+	/*
+	 * msi_desc should be an array one day? For as long as we only support
+	 * 1 MSI vector this is fine.
+	 */
+	struct msi_desc		*msi_desc;
 
 	TAILQ_HEAD(, pci_mmio_region)	mmio;
 };
@@ -345,6 +362,7 @@ struct resource *_lkpi_pci_iomap(struct pci_dev *pdev, int bar, int mmio_size);
 struct pcim_iomap_devres *lkpi_pcim_iomap_devres_find(struct pci_dev *pdev);
 void lkpi_pcim_iomap_table_release(struct device *, void *);
 struct pci_dev *lkpi_pci_get_device(uint16_t, uint16_t, struct pci_dev *);
+struct msi_desc *lkpi_pci_msi_desc_alloc(int);
 
 static inline bool
 dev_is_pci(struct device *dev)
diff --git a/sys/compat/linuxkpi/common/src/linux_pci.c b/sys/compat/linuxkpi/common/src/linux_pci.c
index 57e431a7abcd..da5430eb0d7b 100644
--- a/sys/compat/linuxkpi/common/src/linux_pci.c
+++ b/sys/compat/linuxkpi/common/src/linux_pci.c
@@ -339,6 +339,8 @@ lkpinew_pci_dev_release(struct device *dev)
 	if (pdev->bus->self != pdev)
 		pci_dev_put(pdev->bus->self);
 	free(pdev->bus, M_DEVBUF);
+	if (pdev->msi_desc != NULL)
+		free(pdev->msi_desc, M_DEVBUF);
 	free(pdev, M_DEVBUF);
 }
 
@@ -965,6 +967,35 @@ out:
 	return (-EINVAL);
 }
 
+struct msi_desc *
+lkpi_pci_msi_desc_alloc(int irq)
+{
+	struct device *dev;
+	struct pci_dev *pdev;
+	struct msi_desc *desc;
+	struct pci_devinfo *dinfo;
+	struct pcicfg_msi *msi;
+
+	dev = linux_pci_find_irq_dev(irq);
+	if (dev == NULL)
+		return (NULL);
+
+	pdev = to_pci_dev(dev);
+	if (pdev->msi_desc != NULL)
+		return (pdev->msi_desc);
+
+	dinfo = device_get_ivars(dev->bsddev);
+	msi = &dinfo->cfg.msi;
+
+	desc = malloc(sizeof(*desc), M_DEVBUF, M_WAITOK | M_ZERO);
+
+	desc->msi_attrib.is_64 =
+	   (msi->msi_ctrl & PCIM_MSICTRL_64BIT) ? true : false;
+	desc->msg.data = msi->msi_data;
+
+	return (desc);
+}
+
 CTASSERT(sizeof(dma_addr_t) <= sizeof(uint64_t));
 
 struct linux_dma_obj {
diff --git a/sys/sys/param.h b/sys/sys/param.h
index f0f35e3a268e..07b3e6de80e6 100644
--- a/sys/sys/param.h
+++ b/sys/sys/param.h
@@ -76,7 +76,7 @@
  * cannot include sys/param.h and should only be updated here.
  */
 #undef __FreeBSD_version
-#define __FreeBSD_version 1400077
+#define __FreeBSD_version 1400078
 
 /*
  * __FreeBSD_kernel__ indicates that this system uses the kernel of FreeBSD,