git: 6fc6896c4744 - main - Add bus_get_resource_list functions to the GICv3 driver

From: Andrew Turner <andrew_at_FreeBSD.org>
Date: Fri, 23 Sep 2022 14:47:09 UTC
The branch main has been updated by andrew:

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

commit 6fc6896c474483b3cf34b83fb85f8e9f8cabc8a1
Author:     Andrew Turner <andrew@FreeBSD.org>
AuthorDate: 2022-09-22 10:17:45 +0000
Commit:     Andrew Turner <andrew@FreeBSD.org>
CommitDate: 2022-09-23 14:28:45 +0000

    Add bus_get_resource_list functions to the GICv3 driver
    
    This will be used to reduce code duplication between the ACPI and FDT
    attachments.
    
    Sponsored by:   The FreeBSD Foundation
---
 sys/arm64/arm64/gic_v3_acpi.c | 20 +++++++++++++++++---
 sys/arm64/arm64/gic_v3_fdt.c  | 30 +++++++++++++++++++++++-------
 2 files changed, 40 insertions(+), 10 deletions(-)

diff --git a/sys/arm64/arm64/gic_v3_acpi.c b/sys/arm64/arm64/gic_v3_acpi.c
index cc24b7824ff1..f24662750da7 100644
--- a/sys/arm64/arm64/gic_v3_acpi.c
+++ b/sys/arm64/arm64/gic_v3_acpi.c
@@ -60,6 +60,7 @@ static device_identify_t gic_v3_acpi_identify;
 static device_probe_t gic_v3_acpi_probe;
 static device_attach_t gic_v3_acpi_attach;
 static bus_alloc_resource_t gic_v3_acpi_bus_alloc_res;
+static bus_get_resource_list_t gic_v3_acpi_get_resource_list;
 
 static void gic_v3_acpi_bus_attach(device_t);
 
@@ -72,6 +73,7 @@ static device_method_t gic_v3_acpi_methods[] = {
 	/* Bus interface */
 	DEVMETHOD(bus_alloc_resource,		gic_v3_acpi_bus_alloc_res),
 	DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
+	DEVMETHOD(bus_get_resource_list,	gic_v3_acpi_get_resource_list),
 
 	/* End */
 	DEVMETHOD_END
@@ -447,19 +449,20 @@ static struct resource *
 gic_v3_acpi_bus_alloc_res(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)
 {
-	struct gic_v3_acpi_devinfo *di;
 	struct resource_list_entry *rle;
+	struct resource_list *rl;
 
 	/* We only allocate memory */
 	if (type != SYS_RES_MEMORY)
 		return (NULL);
 
 	if (RMAN_IS_DEFAULT_RANGE(start, end)) {
-		if ((di = device_get_ivars(child)) == NULL)
+		rl = BUS_GET_RESOURCE_LIST(bus, child);
+		if (rl == NULL)
 			return (NULL);
 
 		/* Find defaults for this rid */
-		rle = resource_list_find(&di->di_rl, type, *rid);
+		rle = resource_list_find(rl, type, *rid);
 		if (rle == NULL)
 			return (NULL);
 
@@ -471,3 +474,14 @@ gic_v3_acpi_bus_alloc_res(device_t bus, device_t child, int type, int *rid,
 	return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
 	    count, flags));
 }
+
+static struct resource_list *
+gic_v3_acpi_get_resource_list(device_t bus, device_t child)
+{
+	struct gic_v3_acpi_devinfo *di;
+
+	di = device_get_ivars(child);
+	KASSERT(di != NULL, ("%s: No devinfo", __func__));
+
+	return (&di->di_rl);
+}
diff --git a/sys/arm64/arm64/gic_v3_fdt.c b/sys/arm64/arm64/gic_v3_fdt.c
index 0ca362dc4aea..b0b532a56dec 100644
--- a/sys/arm64/arm64/gic_v3_fdt.c
+++ b/sys/arm64/arm64/gic_v3_fdt.c
@@ -58,6 +58,7 @@ static int gic_v3_fdt_print_child(device_t, device_t);
 static struct resource *gic_v3_ofw_bus_alloc_res(device_t, device_t, int, int *,
     rman_res_t, rman_res_t, rman_res_t, u_int);
 static const struct ofw_bus_devinfo *gic_v3_ofw_get_devinfo(device_t, device_t);
+static bus_get_resource_list_t gic_v3_fdt_get_resource_list;
 
 static device_method_t gic_v3_fdt_methods[] = {
 	/* Device interface */
@@ -68,6 +69,7 @@ static device_method_t gic_v3_fdt_methods[] = {
 	DEVMETHOD(bus_print_child,		gic_v3_fdt_print_child),
 	DEVMETHOD(bus_alloc_resource,		gic_v3_ofw_bus_alloc_res),
 	DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
+	DEVMETHOD(bus_get_resource_list,	gic_v3_fdt_get_resource_list),
 
 	/* ofw_bus interface */
 	DEVMETHOD(ofw_bus_get_devinfo,	gic_v3_ofw_get_devinfo),
@@ -211,11 +213,11 @@ struct gic_v3_ofw_devinfo {
 static int
 gic_v3_fdt_print_child(device_t bus, device_t child)
 {
-	struct gic_v3_ofw_devinfo *di = device_get_ivars(child);
 	struct resource_list *rl;
 	int retval = 0;
 
-	rl = &di->di_rl;
+	rl = BUS_GET_RESOURCE_LIST(bus, child);
+	KASSERT(rl != NULL, ("%s: No resource list", __func__));
 	retval += bus_print_child_header(bus, child);
 	retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#jx");
 	retval += bus_print_child_footer(bus, child);
@@ -238,18 +240,21 @@ static struct resource *
 gic_v3_ofw_bus_alloc_res(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)
 {
-	struct gic_v3_ofw_devinfo *di;
 	struct resource_list_entry *rle;
+	struct resource_list *rl;
 	int ranges_len;
 
+	/* We only allocate memory */
+	if (type != SYS_RES_MEMORY)
+		return (NULL);
+
 	if (RMAN_IS_DEFAULT_RANGE(start, end)) {
-		if ((di = device_get_ivars(child)) == NULL)
-			return (NULL);
-		if (type != SYS_RES_MEMORY)
+		rl = BUS_GET_RESOURCE_LIST(bus, child);
+		if (rl == NULL)
 			return (NULL);
 
 		/* Find defaults for this rid */
-		rle = resource_list_find(&di->di_rl, type, *rid);
+		rle = resource_list_find(rl, type, *rid);
 		if (rle == NULL)
 			return (NULL);
 
@@ -375,3 +380,14 @@ gic_v3_ofw_bus_attach(device_t dev)
 
 	return (bus_generic_attach(dev));
 }
+
+static struct resource_list *
+gic_v3_fdt_get_resource_list(device_t bus, device_t child)
+{
+	struct gic_v3_ofw_devinfo *di;
+
+	di = device_get_ivars(child);
+	KASSERT(di != NULL, ("%s: No devinfo", __func__));
+
+	return (&di->di_rl);
+}