git: e2bef7e30aa7 - main - LinuxKPI: pci: make internal variables file local

From: Bjoern A. Zeeb <bz_at_FreeBSD.org>
Date: Sun, 11 Oct 2026 13:51:32 UTC
The branch main has been updated by bz:

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

commit e2bef7e30aa74ed3d46a5ce5d171993e9e2c14b8
Author:     Bjoern A. Zeeb <bz@FreeBSD.org>
AuthorDate: 2026-06-03 04:32:45 +0000
Commit:     Bjoern A. Zeeb <bz@FreeBSD.org>
CommitDate: 2026-10-11 13:51:03 +0000

    LinuxKPI: pci: make internal variables file local
    
    The only reason the internal lists and lock are shared seems to be
    because the SYSINIT code was in linux_compat.c.
    
    Migrate the three variables (pci_drivers, pci_devices, and pci_lock)
    to linux_pci.c and make them file local.
    
    While doing so adjust a few things:
    (a) mark the various sections of the linux_pci.c file as we add new
        SYSINIT code to the middle of the file.  The file currently really
        contains three parts.
    (b) rename the variables by adding a lkpi_ prefix indicating that they
        are part of our internal code and not part of the public KPI, and
        to clearly distinguish them from any native PCI code upon which
        we call as well.
    (c) factor the locking code out into macros as we have often done in
        various parts of the kernel to make the lock itself more opaque.
    
    No functional changes intended.
    
    Sponsored by:   The FreBSD Foundation
    MFC after:      3 days
    Reviewed by:    emaste, dumbbell (before rebase)
    Differential Revision: https://reviews.freebsd.org/D57432
---
 sys/compat/linuxkpi/common/include/linux/pci.h |  4 --
 sys/compat/linuxkpi/common/src/linux_compat.c  |  7 --
 sys/compat/linuxkpi/common/src/linux_pci.c     | 96 ++++++++++++++++++--------
 3 files changed, 68 insertions(+), 39 deletions(-)

diff --git a/sys/compat/linuxkpi/common/include/linux/pci.h b/sys/compat/linuxkpi/common/include/linux/pci.h
index 3c90e3d9ea9e..b46bb99612c8 100644
--- a/sys/compat/linuxkpi/common/include/linux/pci.h
+++ b/sys/compat/linuxkpi/common/include/linux/pci.h
@@ -295,10 +295,6 @@ struct pci_bus {
 	int		number;
 };
 
-extern struct list_head pci_drivers;
-extern struct list_head pci_devices;
-extern spinlock_t pci_lock;
-
 #define	__devexit_p(x)	x
 
 #define	module_pci_driver(_drv)						\
diff --git a/sys/compat/linuxkpi/common/src/linux_compat.c b/sys/compat/linuxkpi/common/src/linux_compat.c
index 24655eb0341d..08c7cfdac8ed 100644
--- a/sys/compat/linuxkpi/common/src/linux_compat.c
+++ b/sys/compat/linuxkpi/common/src/linux_compat.c
@@ -159,9 +159,6 @@ static cpumask_t *static_single_cpu_mask_lcs;
 struct kobject linux_class_root;
 struct device linux_root_device;
 struct class linux_class_misc;
-struct list_head pci_drivers;
-struct list_head pci_devices;
-spinlock_t pci_lock;
 struct uts_namespace init_uts_ns;
 
 unsigned long linux_timer_hz_mask;
@@ -2969,9 +2966,6 @@ linux_compat_init(void *arg)
 	linux_root_device.bsddev = root_bus;
 	linux_class_misc.name = "misc";
 	class_register(&linux_class_misc);
-	INIT_LIST_HEAD(&pci_drivers);
-	INIT_LIST_HEAD(&pci_devices);
-	spin_lock_init(&pci_lock);
 	init_waitqueue_head(&linux_bit_waitq);
 	init_waitqueue_head(&linux_var_waitq);
 
@@ -3082,7 +3076,6 @@ linux_compat_uninit(void *arg)
 	free(__cpu_data, M_KMALLOC);
 #endif
 
-	spin_lock_destroy(&pci_lock);
 	rw_destroy(&linux_vma_lock);
 }
 SYSUNINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_uninit, NULL);
diff --git a/sys/compat/linuxkpi/common/src/linux_pci.c b/sys/compat/linuxkpi/common/src/linux_pci.c
index 2c442b3d8f77..6b598eb82d76 100644
--- a/sys/compat/linuxkpi/common/src/linux_pci.c
+++ b/sys/compat/linuxkpi/common/src/linux_pci.c
@@ -29,12 +29,10 @@
  */
 
 /*
- * We have two ways to create a pci_dev (pdev):
- * (1) coming from the device_attach DEVMETHOD, and
- * (2) from manual creation via lkpinew_pci_dev(), in which case
- *     we have no driver (pdev->pdrv).
- *
- * Only devices from (1) end up on our LinuxKPI global pci_devices list.
+ * This file contains multiple parts:
+ * (1) PCI
+ * (2) DMA
+ * (3) Backlight
  */
 
 #include <sys/param.h>
@@ -85,6 +83,17 @@
 #include "backlight_if.h"
 #include "pcib_if.h"
 
+/* -------------------------------------------------------------------------- */
+/*
+ * For PCI:
+ * we have two ways to create a pci_dev (pdev):
+ * (1) coming from the device_attach DEVMETHOD, and
+ * (2) from manual creation via lkpinew_pci_dev(), in which case
+ *     we have no driver (pdev->pdrv).
+ *
+ * Only devices from (1) end up on our LinuxKPI global lkpi_pci_devices list.
+ */
+
 /* Undef the linux function macro defined in linux/pci.h */
 #undef pci_get_class
 
@@ -110,6 +119,15 @@ static int linux_backlight_update_status(device_t dev, struct backlight_props *p
 static int linux_backlight_get_info(device_t dev, struct backlight_info *info);
 static void lkpi_pcim_iomap_table_release(struct device *, void *);
 
+static struct list_head lkpi_pci_drivers;
+static struct list_head lkpi_pci_devices;
+static spinlock_t lkpi_pci_lock;
+
+#define	LKPI_PCI_LOCK_INIT()		spin_lock_init(&lkpi_pci_lock)
+#define	LKPI_PCI_LOCK_DESTROY()		spin_lock_destroy(&lkpi_pci_lock)
+#define	LKPI_PCI_LOCK()			spin_lock(&lkpi_pci_lock)
+#define	LKPI_PCI_UNLOCK()		spin_unlock(&lkpi_pci_lock)
+
 static device_method_t pci_methods[] = {
 	DEVMETHOD(device_probe, linux_pci_probe),
 	DEVMETHOD(device_attach, linux_pci_attach),
@@ -299,20 +317,20 @@ linux_pci_find(device_t dev, const struct pci_device_id **idp)
 	subvendor = pci_get_subvendor(dev);
 	subdevice = pci_get_subdevice(dev);
 
-	spin_lock(&pci_lock);
-	list_for_each_entry(pdrv, &pci_drivers, node) {
+	LKPI_PCI_LOCK();
+	list_for_each_entry(pdrv, &lkpi_pci_drivers, node) {
 		for (id = pdrv->id_table; id->vendor != 0; id++) {
 			if (vendor == id->vendor &&
 			    (PCI_ANY_ID == id->device || device == id->device) &&
 			    (PCI_ANY_ID == id->subvendor || subvendor == id->subvendor) &&
 			    (PCI_ANY_ID == id->subdevice || subdevice == id->subdevice)) {
 				*idp = id;
-				spin_unlock(&pci_lock);
+				LKPI_PCI_UNLOCK();
 				return (pdrv);
 			}
 		}
 	}
-	spin_unlock(&pci_lock);
+	LKPI_PCI_UNLOCK();
 	return (NULL);
 }
 
@@ -323,8 +341,8 @@ lkpi_pci_get_device(uint32_t vendor, uint32_t device, struct pci_dev *odev)
 
 	odev0 = odev;
 	found = NULL;
-	spin_lock(&pci_lock);
-	list_for_each_entry(pdev, &pci_devices, links) {
+	LKPI_PCI_LOCK();
+	list_for_each_entry(pdev, &lkpi_pci_devices, links) {
 		/* Walk until we find odev. */
 		if (odev != NULL) {
 			if (pdev == odev)
@@ -339,7 +357,7 @@ lkpi_pci_get_device(uint32_t vendor, uint32_t device, struct pci_dev *odev)
 		}
 	}
 	pci_dev_get(found);
-	spin_unlock(&pci_lock);
+	LKPI_PCI_UNLOCK();
 	pci_dev_put(odev0);
 
 	return (found);
@@ -429,9 +447,9 @@ lkpi_pci_dev_release(struct device *dev)
 	/* Clear the hierarchy recursively to root. */
 	lkpi_pci_dev_bus_release(pdev);
 
-	spin_lock(&pci_lock);
+	LKPI_PCI_LOCK();
 	list_del(&pdev->links);
-	spin_unlock(&pci_lock);
+	LKPI_PCI_UNLOCK();
 
 	linux_pdev_dma_uninit(pdev);
 
@@ -723,9 +741,9 @@ linux_pci_attach_device(device_t dev, struct pci_driver *pdrv,
 	if (error)
 		goto out_err;
 
-	spin_lock(&pci_lock);
-	list_add(&pdev->links, &pci_devices);
-	spin_unlock(&pci_lock);
+	LKPI_PCI_LOCK();
+	list_add(&pdev->links, &lkpi_pci_devices);
+	LKPI_PCI_UNLOCK();
 
 	/*
 	 * Create the hierarchy now as we cannot on demand later.
@@ -1251,9 +1269,9 @@ _linux_pci_register_driver(struct pci_driver *pdrv, devclass_t dc)
 	int error;
 
 	linux_set_current(curthread);
-	spin_lock(&pci_lock);
-	list_add(&pdrv->node, &pci_drivers);
-	spin_unlock(&pci_lock);
+	LKPI_PCI_LOCK();
+	list_add(&pdrv->node, &lkpi_pci_drivers);
+	LKPI_PCI_UNLOCK();
 	if (pdrv->bsddriver.name == NULL)
 		pdrv->bsddriver.name = pdrv->name;
 	pdrv->bsddriver.methods = pci_methods;
@@ -1297,15 +1315,15 @@ lkpi_pci_find_irq_dev(unsigned int irq)
 	struct device *found;
 
 	found = NULL;
-	spin_lock(&pci_lock);
-	list_for_each_entry(pdev, &pci_devices, links) {
+	LKPI_PCI_LOCK();
+	list_for_each_entry(pdev, &lkpi_pci_devices, links) {
 		if (irq == pdev->dev.irq ||
 		    (irq >= pdev->dev.irq_start && irq < pdev->dev.irq_end)) {
 			found = &pdev->dev;
 			break;
 		}
 	}
-	spin_unlock(&pci_lock);
+	LKPI_PCI_UNLOCK();
 	return (found);
 }
 
@@ -1497,9 +1515,9 @@ linux_pci_unregister_driver(struct pci_driver *pdrv)
 
 	bus = devclass_find(pdrv->isdrm ? "vgapci" : "pci");
 
-	spin_lock(&pci_lock);
+	LKPI_PCI_LOCK();
 	list_del(&pdrv->node);
-	spin_unlock(&pci_lock);
+	LKPI_PCI_UNLOCK();
 	bus_topo_lock();
 	if (bus != NULL)
 		devclass_delete_driver(bus, &pdrv->bsddriver);
@@ -1513,9 +1531,9 @@ linux_pci_unregister_drm_driver(struct pci_driver *pdrv)
 
 	bus = devclass_find("vgapci");
 
-	spin_lock(&pci_lock);
+	LKPI_PCI_LOCK();
 	list_del(&pdrv->node);
-	spin_unlock(&pci_lock);
+	LKPI_PCI_UNLOCK();
 	bus_topo_lock();
 	if (bus != NULL)
 		devclass_delete_driver(bus, &pdrv->bsddriver);
@@ -1708,6 +1726,25 @@ linuxkpi_pci_unmap_rom(struct pci_dev *pdev, void *rom)
 	vga_pci_unmap_bios(device_get_parent(dev), rom);
 }
 
+static void
+lkpi_pci_init(void *arg __unused)
+{
+	INIT_LIST_HEAD(&lkpi_pci_drivers);
+	INIT_LIST_HEAD(&lkpi_pci_devices);
+	LKPI_PCI_LOCK_INIT();
+}
+SYSINIT(lkpi_pci_init, SI_SUB_DRIVERS, SI_ORDER_SECOND, lkpi_pci_init, NULL);
+
+static void
+lkpi_pci_uninit(void *arg __unused)
+{
+	LKPI_PCI_LOCK_DESTROY();
+}
+SYSUNINIT(lkpi_pci_uninit, SI_SUB_DRIVERS, SI_ORDER_SECOND, lkpi_pci_uninit, NULL);
+
+/* -------------------------------------------------------------------------- */
+/* DMA */
+
 CTASSERT(sizeof(dma_addr_t) <= sizeof(uint64_t));
 
 struct linux_dma_obj {
@@ -2397,6 +2434,9 @@ linux_dma_pool_free(struct dma_pool *pool, void *vaddr, dma_addr_t dma_addr)
 	uma_zfree_arg(pool->pool_zone, obj, pool);
 }
 
+/* -------------------------------------------------------------------------- */
+/* Backlight. */
+
 static int
 linux_backlight_get_status(device_t dev, struct backlight_props *props)
 {