Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 18 Dec 2025 20:48:07 +0000
From:      John Baldwin <jhb@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: 108b5d90bc1f - stable/14 - bus_alloc_resource: First step in passing resource IDs by value
Message-ID:  <69446887.3c0d5.7d4cc482@gitrepo.freebsd.org>

index | next in thread | raw e-mail

The branch stable/14 has been updated by jhb:

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

commit 108b5d90bc1fa8ed83cfd6f58e636193e03e8049
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2025-10-30 15:14:27 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2025-12-18 20:22:46 +0000

    bus_alloc_resource: First step in passing resource IDs by value
    
    Add wrapper inline functions for bus_alloc_resource* that accept a
    resource ID by value (instead of a pointer).  The rest of the bus
    resource API (outside of compat shims) does not accept resource IDs as
    arguments, but instead obtains the resource ID from the resource
    itself when needed.  As such, there is no reason to return any
    potentially modified resource IDs to the caller.
    
    Reviewed by:    imp
    Differential Revision:  https://reviews.freebsd.org/D53401
    
    (cherry picked from commit 7eb213614b90cde31707a53c4b8ae6acacf2aa0f)
---
 share/man/man9/bus_alloc_resource.9 | 24 +++++++--------------
 sys/kern/subr_bus.c                 |  2 +-
 sys/sys/bus.h                       | 42 +++++++++++++++++++++++++++++++++++++
 sys/sys/param.h                     |  2 +-
 4 files changed, 51 insertions(+), 19 deletions(-)

diff --git a/share/man/man9/bus_alloc_resource.9 b/share/man/man9/bus_alloc_resource.9
index 84a4c9c530c9..5d309229a34e 100644
--- a/share/man/man9/bus_alloc_resource.9
+++ b/share/man/man9/bus_alloc_resource.9
@@ -26,7 +26,7 @@
 .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 .\"
-.Dd May 20, 2016
+.Dd October 30, 2025
 .Dt BUS_ALLOC_RESOURCE 9
 .Os
 .Sh NAME
@@ -43,14 +43,14 @@
 .In machine/resource.h
 .Ft struct resource *
 .Fo bus_alloc_resource
-.Fa "device_t dev" "int type" "int *rid" "rman_res_t start" "rman_res_t end"
+.Fa "device_t dev" "int type" "int rid" "rman_res_t start" "rman_res_t end"
 .Fa "rman_res_t count" "u_int flags"
 .Fc
 .Ft struct resource *
-.Fn bus_alloc_resource_any "device_t dev" "int type" "int *rid" "u_int flags"
+.Fn bus_alloc_resource_any "device_t dev" "int type" "int rid" "u_int flags"
 .Ft struct resource *
 .Fo bus_alloc_resource_anywhere
-.Fa "device_t dev" "int type" "int *rid" "rman_res_t count" "u_int flags"
+.Fa "device_t dev" "int type" "int rid" "rman_res_t count" "u_int flags"
 .Fc
 .Sh DESCRIPTION
 This is an easy interface to the resource-management functions.
@@ -106,15 +106,13 @@ for I/O memory
 .El
 .It
 .Fa rid
-points to a bus specific handle that identifies the resource being allocated.
+is a bus specific handle that identifies the resource being allocated.
 For ISA this is an index into an array of resources that have been setup
 for this device by either the PnP mechanism, or via the hints mechanism.
 For PCCARD, this is an index into the array of resources described by the PC Card's
 CIS entry.
 For PCI, the offset into PCI config space which has the BAR to use to access
 the resource.
-The bus methods are free to change the RIDs that they are given as a parameter.
-You must not depend on the value you gave it earlier.
 .It
 .Fa start
 and
@@ -175,20 +173,12 @@ A pointer to
 is returned on success, a null pointer otherwise.
 .Sh EXAMPLES
 This is some example code that allocates a 32 byte I/O port range and an IRQ.
-The values of
-.Va portid
-and
-.Va irqid
-should be saved in the softc of the device after these calls.
 .Bd -literal
 	struct resource *portres, *irqres;
-	int portid, irqid;
 
-	portid = 0;
-	irqid = 0;
-	portres = bus_alloc_resource(dev, SYS_RES_IOPORT, &portid,
+	portres = bus_alloc_resource(dev, SYS_RES_IOPORT, 0,
 			0ul, ~0ul, 32, RF_ACTIVE);
-	irqres = bus_alloc_resource_any(dev, SYS_RES_IRQ, &irqid,
+	irqres = bus_alloc_resource_any(dev, SYS_RES_IRQ, 0,
 			RF_ACTIVE | RF_SHAREABLE);
 .Ed
 .Sh SEE ALSO
diff --git a/sys/kern/subr_bus.c b/sys/kern/subr_bus.c
index ce7209e5f852..078ea7002013 100644
--- a/sys/kern/subr_bus.c
+++ b/sys/kern/subr_bus.c
@@ -4676,7 +4676,7 @@ bus_release_resources(device_t dev, const struct resource_spec *rs,
  * parent of @p dev.
  */
 struct resource *
-bus_alloc_resource(device_t dev, int type, int *rid, rman_res_t start,
+(bus_alloc_resource)(device_t dev, int type, int *rid, rman_res_t start,
     rman_res_t end, rman_res_t count, u_int flags)
 {
 	struct resource *res;
diff --git a/sys/sys/bus.h b/sys/sys/bus.h
index a2189e068293..a9b22cd5d6cb 100644
--- a/sys/sys/bus.h
+++ b/sys/sys/bus.h
@@ -617,6 +617,48 @@ bus_alloc_resource_anywhere(device_t dev, int type, int *rid,
 	return (bus_alloc_resource(dev, type, rid, 0, ~0, count, flags));
 }
 
+/* Compat shims for bus_alloc_resource API. */
+static __inline struct resource *
+bus_alloc_resource_const(device_t dev, int type, int rid, rman_res_t start,
+    rman_res_t end, rman_res_t count, u_int flags)
+{
+	return (bus_alloc_resource(dev, type, &rid, start, end, count, flags));
+}
+
+static __inline struct resource *
+bus_alloc_resource_any_const(device_t dev, int type, int rid, u_int flags)
+{
+	return (bus_alloc_resource(dev, type, &rid, 0, ~0, 1, flags));
+}
+
+static __inline struct resource *
+bus_alloc_resource_anywhere_const(device_t dev, int type, int rid,
+    rman_res_t count, u_int flags)
+{
+	return (bus_alloc_resource(dev, type, &rid, 0, ~0, count, flags));
+}
+
+#define	bus_alloc_resource(dev, type, rid, start, end, count, flags)	\
+	_Generic((rid),							\
+	    int *: bus_alloc_resource,					\
+	    unsigned int *: bus_alloc_resource,				\
+	    default: bus_alloc_resource_const)				\
+	((dev), (type), (rid), (start), (end), (count), (flags))
+
+#define	bus_alloc_resource_any(dev, type, rid, flags)			\
+	_Generic((rid),							\
+	    int *: bus_alloc_resource_any,				\
+	    unsigned int *: bus_alloc_resource_any,			\
+	    default: bus_alloc_resource_any_const)			\
+	((dev), (type), (rid), (flags))
+
+#define	bus_alloc_resource_anywhere(dev, type, rid, count, flags)	\
+	_Generic((rid),							\
+	    int *: bus_alloc_resource_anywhere,				\
+	    unsigned int *: bus_alloc_resource_anywhere,		\
+	    default: bus_alloc_resource_anywhere_const)			\
+	((dev), (type), (rid), (count), (flags))
+
 /* Compat shims for simpler bus resource API. */
 int	bus_adjust_resource_new(device_t child, struct resource *r,
     rman_res_t start, rman_res_t end);
diff --git a/sys/sys/param.h b/sys/sys/param.h
index 26f201a21715..73f122785266 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 1403506
+#define __FreeBSD_version 1403507
 
 /*
  * __FreeBSD_kernel__ indicates that this system uses the kernel of FreeBSD,


help

Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?69446887.3c0d5.7d4cc482>