git: 3a48dfe115f3 - main - bhnd_chipc: Make use of bus_generic_rman_* to simplify some code

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Tue, 27 Feb 2024 19:45:07 UTC
The branch main has been updated by jhb:

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

commit 3a48dfe115f35abac6c2d9ec03a6a574baeaf0bc
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2024-02-27 19:44:22 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2024-02-27 19:44:22 +0000

    bhnd_chipc: Make use of bus_generic_rman_* to simplify some code
    
    This uses bus_generic_rman_alloc/release_resource to reduce some code
    duplication.  However, I've left the custom activate/deactivate
    methods as-is.
    
    Differential Revision:  https://reviews.freebsd.org/D43939
---
 sys/dev/bhnd/cores/chipc/chipc.c | 68 +++++++++++++---------------------------
 1 file changed, 22 insertions(+), 46 deletions(-)

diff --git a/sys/dev/bhnd/cores/chipc/chipc.c b/sys/dev/bhnd/cores/chipc/chipc.c
index 0510a3195015..f99f42fb3ac3 100644
--- a/sys/dev/bhnd/cores/chipc/chipc.c
+++ b/sys/dev/bhnd/cores/chipc/chipc.c
@@ -123,13 +123,13 @@ static void		 chipc_disable_otp_power(struct chipc_softc *sc);
 static int		 chipc_enable_sprom_pins(struct chipc_softc *sc);
 static void		 chipc_disable_sprom_pins(struct chipc_softc *sc);
 
-static int		 chipc_try_activate_resource(struct chipc_softc *sc,
+static int		 chipc_try_activate_resource(device_t dev,
 			     device_t child, int type, int rid,
 			     struct resource *r, bool req_direct);
 
 static int		 chipc_init_rman(struct chipc_softc *sc);
 static void		 chipc_free_rman(struct chipc_softc *sc);
-static struct rman	*chipc_get_rman(struct chipc_softc *sc, int type);
+static struct rman	*chipc_get_rman(device_t dev, int type, u_int flags);
 
 /* quirk and capability flag convenience macros */
 #define	CHIPC_QUIRK(_sc, _name)	\
@@ -747,10 +747,13 @@ chipc_free_rman(struct chipc_softc *sc)
  * 
  * @param sc The chipc device state.
  * @param type The resource type (e.g. SYS_RES_MEMORY, SYS_RES_IRQ, ...)
+ * @param flags Resource flags (e.g. RF_PREFETCHABLE)
  */
 static struct rman *
-chipc_get_rman(struct chipc_softc *sc, int type)
+chipc_get_rman(device_t dev, int type, u_int flags)
 {	
+	struct chipc_softc *sc = device_get_softc(dev);
+
 	switch (type) {
 	case SYS_RES_MEMORY:
 		return (&sc->mem_rman);
@@ -782,7 +785,7 @@ chipc_alloc_resource(device_t dev, device_t child, int type,
 	rle = NULL;
 
 	/* Fetch the resource manager, delegate request if necessary */
-	rm = chipc_get_rman(sc, type);
+	rm = chipc_get_rman(dev, type, flags);
 	if (rm == NULL) {
 		/* Requested resource type is delegated to our parent */
 		rv = bus_generic_rl_alloc_resource(dev, child, type, rid,
@@ -851,31 +854,13 @@ chipc_alloc_resource(device_t dev, device_t child, int type,
 		return (NULL);
 
 	/* Make our rman reservation */
-	rv = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE,
-	    child);
+	rv = bus_generic_rman_alloc_resource(dev, child, type, rid, start, end,
+	    count, flags);
 	if (rv == NULL) {
 		chipc_release_region(sc, cr, RF_ALLOCATED);
 		return (NULL);
 	}
 
-	rman_set_rid(rv, *rid);
-
-	/* Activate */
-	if (flags & RF_ACTIVE) {
-		error = bus_activate_resource(child, type, *rid, rv);
-		if (error) {
-			device_printf(dev,
-			    "failed to activate entry %#x type %d for "
-				"child %s: %d\n",
-			     *rid, type, device_get_nameunit(child), error);
-
-			chipc_release_region(sc, cr, RF_ALLOCATED);
-			rman_release_resource(rv);
-
-			return (NULL);
-		}
-	}
-
 	/* Update child's resource list entry */
 	if (rle != NULL) {
 		rle->res = rv;
@@ -900,7 +885,7 @@ chipc_release_resource(device_t dev, device_t child, int type, int rid,
 	sc = device_get_softc(dev);
 
 	/* Handled by parent bus? */
-	rm = chipc_get_rman(sc, type);
+	rm = chipc_get_rman(dev, type, rman_get_flags(r));
 	if (rm == NULL || !rman_is_region_manager(r, rm)) {
 		return (bus_generic_rl_release_resource(dev, child, type, rid,
 		    r));
@@ -912,13 +897,8 @@ chipc_release_resource(device_t dev, device_t child, int type, int rid,
 		return (EINVAL);
 
 	/* Deactivate resources */
-	if (rman_get_flags(r) & RF_ACTIVE) {
-		error = BUS_DEACTIVATE_RESOURCE(dev, child, type, rid, r);
-		if (error)
-			return (error);
-	}
-
-	if ((error = rman_release_resource(r)))
+	error = bus_generic_rman_release_resource(dev, child, type, rid, r);
+	if (error != 0)
 		return (error);
 
 	/* Drop allocation reference */
@@ -943,7 +923,7 @@ chipc_adjust_resource(device_t dev, device_t child, int type,
 	sc = device_get_softc(dev);
 
 	/* Handled by parent bus? */
-	rm = chipc_get_rman(sc, type);
+	rm = chipc_get_rman(dev, type, rman_get_flags(r));
 	if (rm == NULL || !rman_is_region_manager(r, rm)) {
 		return (bus_generic_adjust_resource(dev, child, type, r, start,
 		    end));
@@ -978,16 +958,17 @@ chipc_adjust_resource(device_t dev, device_t child, int type,
  * as RF_ACTIVE if bhnd direct resource allocation fails.
  */
 static int
-chipc_try_activate_resource(struct chipc_softc *sc, device_t child, int type,
+chipc_try_activate_resource(device_t dev, device_t child, int type,
     int rid, struct resource *r, bool req_direct)
 {
+	struct chipc_softc	*sc = device_get_softc(dev);
 	struct rman		*rm;
 	struct chipc_region	*cr;
 	bhnd_size_t		 cr_offset;
 	rman_res_t		 r_start, r_end, r_size;
 	int			 error;
 
-	rm = chipc_get_rman(sc, type);
+	rm = chipc_get_rman(dev, type, rman_get_flags(r));
 	if (rm == NULL || !rman_is_region_manager(r, rm))
 		return (EINVAL);
 
@@ -1033,21 +1014,18 @@ static int
 chipc_activate_bhnd_resource(device_t dev, device_t child, int type,
     int rid, struct bhnd_resource *r)
 {
-	struct chipc_softc	*sc;
 	struct rman		*rm;
 	int			 error;
 
-	sc = device_get_softc(dev);
-
 	/* Delegate non-locally managed resources to parent */
-	rm = chipc_get_rman(sc, type);
+	rm = chipc_get_rman(dev, type, rman_get_flags(r->res));
 	if (rm == NULL || !rman_is_region_manager(r->res, rm)) {
 		return (bhnd_bus_generic_activate_resource(dev, child, type,
 		    rid, r));
 	}
 
 	/* Try activating the chipc region resource */
-	error = chipc_try_activate_resource(sc, child, type, rid, r->res,
+	error = chipc_try_activate_resource(dev, child, type, rid, r->res,
 	    false);
 	if (error)
 		return (error);
@@ -1064,20 +1042,17 @@ static int
 chipc_activate_resource(device_t dev, device_t child, int type, int rid,
     struct resource *r)
 {
-	struct chipc_softc	*sc;
 	struct rman		*rm;
 
-	sc = device_get_softc(dev);
-
 	/* Delegate non-locally managed resources to parent */
-	rm = chipc_get_rman(sc, type);
+	rm = chipc_get_rman(dev, type, rman_get_flags(r));
 	if (rm == NULL || !rman_is_region_manager(r, rm)) {
 		return (bus_generic_activate_resource(dev, child, type, rid,
 		    r));
 	}
 
 	/* Try activating the chipc region-based resource */
-	return (chipc_try_activate_resource(sc, child, type, rid, r, true));
+	return (chipc_try_activate_resource(dev, child, type, rid, r, true));
 }
 
 /**
@@ -1095,7 +1070,7 @@ chipc_deactivate_resource(device_t dev, device_t child, int type,
 	sc = device_get_softc(dev);
 
 	/* Handled by parent bus? */
-	rm = chipc_get_rman(sc, type);
+	rm = chipc_get_rman(dev, type, rman_get_flags(r));
 	if (rm == NULL || !rman_is_region_manager(r, rm)) {
 		return (bus_generic_deactivate_resource(dev, child, type, rid,
 		    r));
@@ -1396,6 +1371,7 @@ static device_method_t chipc_methods[] = {
 	DEVMETHOD(bus_activate_resource,	chipc_activate_resource),
 	DEVMETHOD(bus_deactivate_resource,	chipc_deactivate_resource),
 	DEVMETHOD(bus_get_resource_list,	chipc_get_resource_list),
+	DEVMETHOD(bus_get_rman,			chipc_get_rman),
 
 	DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
 	DEVMETHOD(bus_teardown_intr,		bus_generic_teardown_intr),