git: faeb273e7a8e - main - arm nexus: Use bus_generic_rman_*

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Tue, 23 Jan 2024 17:45:59 UTC
The branch main has been updated by jhb:

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

commit faeb273e7a8ec57d87de8d4337598fb11ae305c3
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2024-01-23 17:37:13 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2024-01-23 17:37:13 +0000

    arm nexus: Use bus_generic_rman_*
    
    - Implement bus_get_rman pulling bits from nexus_alloc_resource.
    
    - Implement bus_map/unmap_resource pulling bits from
      nexus_activate/deactivate_resource.
    
    - Use bus_generic_rman_* for
      bus_alloc/adjust/activate/deactivate/release_resource except for
      custom interrupt activate/deactivate logic still in
      nexus_activate/deactivate_resource.
    
    Reviewed by:    imp
    Differential Revision:  https://reviews.freebsd.org/D43437
---
 sys/arm/arm/nexus.c | 220 +++++++++++++++++++++++-----------------------------
 1 file changed, 97 insertions(+), 123 deletions(-)

diff --git a/sys/arm/arm/nexus.c b/sys/arm/arm/nexus.c
index 196e879fb0db..eb0d1a5b7ded 100644
--- a/sys/arm/arm/nexus.c
+++ b/sys/arm/arm/nexus.c
@@ -83,10 +83,10 @@ static bus_add_child_t		nexus_add_child;
 static bus_print_child_t	nexus_print_child;
 
 static bus_activate_resource_t	nexus_activate_resource;
-static bus_adjust_resource_t	nexus_adjust_resource;
-static bus_alloc_resource_t	nexus_alloc_resource;
 static bus_deactivate_resource_t nexus_deactivate_resource;
-static bus_release_resource_t	nexus_release_resource;
+static bus_get_rman_t		nexus_get_rman;
+static bus_map_resource_t	nexus_map_resource;
+static bus_unmap_resource_t	nexus_unmap_resource;
 
 #ifdef SMP
 static bus_bind_intr_t		nexus_bind_intr;
@@ -119,10 +119,13 @@ static device_method_t nexus_methods[] = {
 	DEVMETHOD(bus_add_child,	nexus_add_child),
 	DEVMETHOD(bus_print_child,	nexus_print_child),
 	DEVMETHOD(bus_activate_resource, nexus_activate_resource),
-	DEVMETHOD(bus_adjust_resource,	nexus_adjust_resource),
-	DEVMETHOD(bus_alloc_resource,	nexus_alloc_resource),
+	DEVMETHOD(bus_adjust_resource,	bus_generic_rman_adjust_resource),
+	DEVMETHOD(bus_alloc_resource,	bus_generic_rman_alloc_resource),
 	DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource),
-	DEVMETHOD(bus_release_resource,	nexus_release_resource),
+	DEVMETHOD(bus_get_rman,		nexus_get_rman),
+	DEVMETHOD(bus_map_resource,	nexus_map_resource),
+	DEVMETHOD(bus_release_resource,	bus_generic_rman_release_resource),
+	DEVMETHOD(bus_unmap_resource,	nexus_unmap_resource),
 #ifdef SMP
 	DEVMETHOD(bus_bind_intr,	nexus_bind_intr),
 #endif
@@ -216,83 +219,18 @@ nexus_add_child(device_t bus, u_int order, const char *name, int unit)
 	return (child);
 }
 
-/*
- * Allocate a resource on behalf of child.  NB: child is usually going to be a
- * child of one of our descendants, not a direct child of nexus0.
- */
-static struct resource *
-nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
-    rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
+static struct rman *
+nexus_get_rman(device_t bus, int type, u_int flags)
 {
-	struct resource *rv;
-	struct rman *rm;
-	int needactivate = flags & RF_ACTIVE;
-
-	flags &= ~RF_ACTIVE;
-
 	switch (type) {
 	case SYS_RES_IRQ:
-		rm = &irq_rman;
-		break;
-
+		return (&irq_rman);
 	case SYS_RES_MEMORY:
 	case SYS_RES_IOPORT:
-		rm = &mem_rman;
-		break;
-
+		return (&mem_rman);
 	default:
 		return (NULL);
 	}
-
-	rv = rman_reserve_resource(rm, start, end, count, flags, child);
-	if (rv == NULL)
-		return (NULL);
-
-	rman_set_rid(rv, *rid);
-
-	if (needactivate) {
-		if (bus_activate_resource(child, type, *rid, rv)) {
-			rman_release_resource(rv);
-			return (NULL);
-		}
-	}
-
-	return (rv);
-}
-
-static int
-nexus_adjust_resource(device_t bus __unused, device_t child __unused, int type,
-    struct resource *r, rman_res_t start, rman_res_t end)
-{
-	struct rman *rm;
-
-	switch (type) {
-	case SYS_RES_IRQ:
-		rm = &irq_rman;
-		break;
-	case SYS_RES_MEMORY:
-		rm = &mem_rman;
-		break;
-	default:
-		return (EINVAL);
-	}
-	if (rman_is_region_manager(r, rm) == 0)
-		return (EINVAL);
-	return (rman_adjust_resource(r, start, end));
-}
-
-static int
-nexus_release_resource(device_t bus, device_t child, int type, int rid,
-    struct resource *res)
-{
-	int error;
-
-	if (rman_get_flags(res) & RF_ACTIVE) {
-		error = bus_deactivate_resource(child, type, rid, res);
-		if (error)
-			return (error);
-	}
-	return (rman_release_resource(res));
 }
 
 static bus_space_tag_t
@@ -371,73 +309,109 @@ nexus_activate_resource(device_t bus, device_t child, int type, int rid,
     struct resource *r)
 {
 	int err;
-	bus_addr_t paddr;
-	bus_size_t psize;
-	bus_space_handle_t vaddr;
-
-	if ((err = rman_activate_resource(r)) != 0)
-		return (err);
 
-	/*
-	 * If this is a memory resource, map it into the kernel.
-	 */
-	if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
-		paddr = (bus_addr_t)rman_get_start(r);
-		psize = (bus_size_t)rman_get_size(r);
-#ifdef FDT
-		err = bus_space_map(fdtbus_bs_tag, paddr, psize, 0, &vaddr);
-		if (err != 0) {
-			rman_deactivate_resource(r);
+	switch (type) {
+	case SYS_RES_MEMORY:
+	case SYS_RES_IOPORT:
+		return (bus_generic_rman_activate_resource(bus, child, type,
+		    rid, r));
+	case SYS_RES_IRQ:
+		err = rman_activate_resource(r);
+		if (err != 0)
 			return (err);
-		}
-		rman_set_bustag(r, fdtbus_bs_tag);
-#else
-		vaddr = (bus_space_handle_t)pmap_mapdev((vm_paddr_t)paddr,
-		    (vm_size_t)psize);
-		if (vaddr == 0) {
-			rman_deactivate_resource(r);
-			return (ENOMEM);
-		}
-		rman_set_bustag(r, (void *)1);
-#endif
-		rman_set_virtual(r, (void *)vaddr);
-		rman_set_bushandle(r, vaddr);
-		return (0);
-	} else if (type == SYS_RES_IRQ) {
 		err = intr_activate_irq(child, r);
 		if (err != 0) {
 			rman_deactivate_resource(r);
 			return (err);
 		}
+		return (0);
+	default:
+		return (EINVAL);
 	}
-	return (0);
 }
 
 static int
-nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
-    struct resource *r)
+nexus_map_resource(device_t bus, device_t child, int type, struct resource *r,
+    struct resource_map_request *argsp, struct resource_map *map)
 {
-	bus_size_t psize;
-	bus_space_handle_t vaddr;
+	struct resource_map_request args;
+	rman_res_t length, start;
+	int error;
+
+	/* Resources must be active to be mapped. */
+	if (!(rman_get_flags(r) & RF_ACTIVE))
+		return (ENXIO);
+
+	switch (type) {
+	case SYS_RES_MEMORY:
+	case SYS_RES_IOPORT:
+		break;
+	default:
+		return (EINVAL);
+	}
 
-	if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
-		psize = (bus_size_t)rman_get_size(r);
-		vaddr = rman_get_bushandle(r);
+	resource_init_map_request(&args);
+	error = resource_validate_map_request(r, argsp, &args, &start, &length);
+	if (error)
+		return (error);
 
-		if (vaddr != 0) {
 #ifdef FDT
-			bus_space_unmap(fdtbus_bs_tag, vaddr, psize);
+	error = bus_space_map(fdtbus_bs_tag, start, length, 0,
+	    &map->r_bushandle);
+	if (error)
+		return (error);
+	map->r_bustag = fdtbus_bs_tag;
+	map->r_vaddr = (void *)map->r_bushandle;
 #else
-			pmap_unmapdev((void *)vaddr, (vm_size_t)psize);
+	map->r_vaddr = pmap_mapdev(start, length);
+	if (map->r_vaddr == NULL)
+		return (ENOMEM);
+	map->r_bustag = (void *)1;
+	map->r_bushandle = (bus_space_handle_t)map->r_vaddr;
 #endif
-			rman_set_virtual(r, NULL);
-			rman_set_bushandle(r, 0);
-		}
-	} else if (type == SYS_RES_IRQ) {
-		intr_deactivate_irq(child, r);
+	map->r_size = length;
+	return (0);
+}
+
+static int
+nexus_unmap_resource(device_t bus, device_t child, int type, struct resource *r,
+    struct resource_map *map)
+{
+
+	switch (type) {
+	case SYS_RES_MEMORY:
+	case SYS_RES_IOPORT:
+#ifdef FDT
+		bus_space_unmap(map->r_bustag, map->r_bushandle, map->r_size);
+#else
+		pmap_unmapdev(map->r_vaddr, map->r_size);
+#endif
+		return (0);
+	default:
+		return (EINVAL);
 	}
+}
+
+static int
+nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
+    struct resource *r)
+{
+	int error;
 
-	return (rman_deactivate_resource(r));
+	switch (type) {
+	case SYS_RES_MEMORY:
+	case SYS_RES_IOPORT:
+		return (bus_generic_rman_deactivate_resource(bus, child, type,
+		    rid, r));
+	case SYS_RES_IRQ:
+		error = rman_deactivate_resource(r);
+		if (error)
+			return (error);
+		intr_deactivate_irq(child, r);
+		return (0);
+	default:
+		return (EINVAL);
+	}
 }
 
 #ifdef FDT