git: f224006fe1b3 - main - Allocate resources selectively. One of the SMMU interrupt lines (priq) is optional and may be ommited in FDT.

From: Ruslan Bukin <br_at_FreeBSD.org>
Date: Mon, 09 May 2022 12:34:27 UTC
The branch main has been updated by br:

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

commit f224006fe1b367452fc3f2d36b32411cbbe11101
Author:     Ruslan Bukin <br@FreeBSD.org>
AuthorDate: 2022-05-09 12:30:37 +0000
Commit:     Ruslan Bukin <br@FreeBSD.org>
CommitDate: 2022-05-09 12:30:37 +0000

    Allocate resources selectively.
    One of the SMMU interrupt lines (priq) is optional and may be ommited in FDT.
    
    Tested on ARM Morello Board, which has three SMMU units: first two have four
    interrupt lines, last one has three interrupt lines.
    
    Sponsored by: UKRI
---
 sys/arm64/iommu/smmu.c      |  8 +-----
 sys/arm64/iommu/smmu_acpi.c | 39 ++++++++++++++++++++++++++
 sys/arm64/iommu/smmu_fdt.c  | 68 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 108 insertions(+), 7 deletions(-)

diff --git a/sys/arm64/iommu/smmu.c b/sys/arm64/iommu/smmu.c
index f3e434c02e3f..e1197db34375 100644
--- a/sys/arm64/iommu/smmu.c
+++ b/sys/arm64/iommu/smmu.c
@@ -152,7 +152,7 @@ __FBSDID("$FreeBSD$");
 static struct resource_spec smmu_spec[] = {
 	{ SYS_RES_MEMORY, 0, RF_ACTIVE },
 	{ SYS_RES_IRQ, 0, RF_ACTIVE },
-	{ SYS_RES_IRQ, 1, RF_ACTIVE },
+	{ SYS_RES_IRQ, 1, RF_ACTIVE | RF_OPTIONAL },
 	{ SYS_RES_IRQ, 2, RF_ACTIVE },
 	{ SYS_RES_IRQ, 3, RF_ACTIVE },
 	RESOURCE_SPEC_END
@@ -1547,12 +1547,6 @@ smmu_attach(device_t dev)
 
 	mtx_init(&sc->sc_mtx, device_get_nameunit(sc->dev), "smmu", MTX_DEF);
 
-	error = bus_alloc_resources(dev, smmu_spec, sc->res);
-	if (error) {
-		device_printf(dev, "Couldn't allocate resources.\n");
-		return (ENXIO);
-	}
-
 	error = smmu_setup_interrupts(sc);
 	if (error) {
 		bus_release_resources(dev, smmu_spec, sc->res);
diff --git a/sys/arm64/iommu/smmu_acpi.c b/sys/arm64/iommu/smmu_acpi.c
index 6ddbb0138c87..bd59ad398e6a 100644
--- a/sys/arm64/iommu/smmu_acpi.c
+++ b/sys/arm64/iommu/smmu_acpi.c
@@ -40,6 +40,7 @@ __FBSDID("$FreeBSD$");
 #include <sys/bus.h>
 #include <sys/bitstring.h>
 #include <sys/kernel.h>
+#include <sys/rman.h>
 #include <sys/tree.h>
 #include <sys/taskqueue.h>
 #include <sys/malloc.h>
@@ -192,6 +193,7 @@ smmu_acpi_attach(device_t dev)
 	struct iommu_unit *iommu;
 	uintptr_t priv;
 	int err;
+	int rid;
 
 	sc = device_get_softc(dev);
 	sc->dev = dev;
@@ -204,6 +206,43 @@ smmu_acpi_attach(device_t dev)
 		device_printf(sc->dev, "%s: features %x\n",
 		    __func__, sc->features);
 
+	rid = 0;
+	sc->res[0] = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
+	    RF_ACTIVE);
+	if (sc->res[0] == NULL) {
+		device_printf(dev, "Can't allocate memory resource.\n");
+		err = ENXIO;
+		goto error;
+	}
+
+	/*
+	 * Interrupt lines are "eventq", "priq", "cmdq-sync", "gerror".
+	 */
+
+	rid = 0;
+	sc->res[1] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
+	if (sc->res[1] == NULL) {
+		device_printf(dev, "Can't allocate eventq IRQ resource.\n");
+		err = ENXIO;
+		goto error;
+	}
+
+	rid = 2;
+	sc->res[3] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
+	if (sc->res[3] == NULL) {
+		device_printf(dev, "Can't allocate cmdq-sync IRQ resource.\n");
+		err = ENXIO;
+		goto error;
+	}
+
+	rid = 3;
+	sc->res[4] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
+	if (sc->res[4] == NULL) {
+		device_printf(dev, "Can't allocate gerror IRQ resource.\n");
+		err = ENXIO;
+		goto error;
+	}
+
 	err = smmu_attach(dev);
 	if (err != 0)
 		goto error;
diff --git a/sys/arm64/iommu/smmu_fdt.c b/sys/arm64/iommu/smmu_fdt.c
index 86cf0f9fd0cf..f2d441fe8340 100644
--- a/sys/arm64/iommu/smmu_fdt.c
+++ b/sys/arm64/iommu/smmu_fdt.c
@@ -82,11 +82,79 @@ smmu_fdt_attach(device_t dev)
 	struct smmu_softc *sc;
 	struct smmu_unit *unit;
 	struct iommu_unit *iommu;
+	phandle_t node;
 	int err;
+	int rid;
 
 	sc = device_get_softc(dev);
 	sc->dev = dev;
 
+	node = ofw_bus_get_node(dev);
+
+	rid = 0;
+	sc->res[0] = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
+	    RF_ACTIVE);
+	if (sc->res[0] == NULL) {
+		device_printf(dev, "Can't allocate memory resource.\n");
+		err = ENXIO;
+		goto error;
+	}
+
+	/*
+	 * Interrupt lines are "eventq", "priq", "cmdq-sync", "gerror".
+	 */
+
+	err = ofw_bus_find_string_index(node, "interrupt-names", "eventq",
+	    &rid);
+	if (err != 0) {
+		device_printf(dev, "Can't get eventq IRQ.\n");
+		err = ENXIO;
+		goto error;
+	}
+
+	sc->res[1] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
+	if (sc->res[1] == NULL) {
+		device_printf(dev, "Can't allocate eventq IRQ resource.\n");
+		err = ENXIO;
+		goto error;
+	}
+
+	/*
+	 * sc->res[2] is reserved for priq IRQ. It is optional and not used
+	 * by the SMMU driver. This IRQ line may or may not be provided by
+	 * hardware.
+	 */
+
+	err = ofw_bus_find_string_index(node, "interrupt-names", "cmdq-sync",
+	    &rid);
+	if (err != 0) {
+		device_printf(dev, "Can't get cmdq-sync IRQ.\n");
+		err = ENXIO;
+		goto error;
+	}
+
+	sc->res[3] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
+	if (sc->res[3] == NULL) {
+		device_printf(dev, "Can't allocate cmdq-sync IRQ resource.\n");
+		err = ENXIO;
+		goto error;
+	}
+
+	err = ofw_bus_find_string_index(node, "interrupt-names", "gerror",
+	    &rid);
+	if (err != 0) {
+		device_printf(dev, "Can't get gerror IRQ.\n");
+		err = ENXIO;
+		goto error;
+	}
+
+	sc->res[4] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
+	if (sc->res[4] == NULL) {
+		device_printf(dev, "Can't allocate gerror IRQ resource.\n");
+		err = ENXIO;
+		goto error;
+	}
+
 	err = smmu_attach(dev);
 	if (err != 0)
 		goto error;