git: 3e19edca9eb1 - main - dev/psci: Make SMCCC into a real driver

From: Andrew Turner <andrew_at_FreeBSD.org>
Date: Tue, 15 Oct 2024 17:25:11 UTC
The branch main has been updated by andrew:

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

commit 3e19edca9eb1402afe330cb1d9d562dedca2d95a
Author:     Andrew Turner <andrew@FreeBSD.org>
AuthorDate: 2024-10-14 14:33:59 +0000
Commit:     Andrew Turner <andrew@FreeBSD.org>
CommitDate: 2024-10-15 17:24:42 +0000

    dev/psci: Make SMCCC into a real driver
    
    This will be used by other drivers that manage SMCCC firmware services
    to use as an attachment point.
    
    Sponsored by:   Arm Ltd
    Differential Revision:  https://reviews.freebsd.org/D46988
---
 sys/dev/psci/psci.c  | 10 ++++++++--
 sys/dev/psci/smccc.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+), 2 deletions(-)

diff --git a/sys/dev/psci/psci.c b/sys/dev/psci/psci.c
index 3211c331ed6e..e445e90956ac 100644
--- a/sys/dev/psci/psci.c
+++ b/sys/dev/psci/psci.c
@@ -71,6 +71,7 @@
 
 struct psci_softc {
 	device_t        dev;
+	device_t	smccc_dev;
 
 	uint32_t	psci_version;
 	uint32_t	psci_fnids[PSCI_FN_MAX];
@@ -341,11 +342,16 @@ psci_attach(device_t dev, psci_initfn_t psci_init, int default_version)
 	if (psci_init(dev, default_version))
 		return (ENXIO);
 
+	psci_softc = sc;
+
 #ifdef __aarch64__
 	smccc_init();
-#endif
+	sc->smccc_dev = device_add_child(dev, "smccc", DEVICE_UNIT_ANY);
+	if (sc->smccc_dev == NULL)
+		device_printf(dev, "Unable to add SMCCC device\n");
 
-	psci_softc = sc;
+	bus_generic_attach(dev);
+#endif
 
 	return (0);
 }
diff --git a/sys/dev/psci/smccc.c b/sys/dev/psci/smccc.c
index 21a6bd54e650..0cd3255fc911 100644
--- a/sys/dev/psci/smccc.c
+++ b/sys/dev/psci/smccc.c
@@ -34,7 +34,9 @@
 
 #include <sys/param.h>
 #include <sys/systm.h>
+#include <sys/bus.h>
 #include <sys/kernel.h>
+#include <sys/module.h>
 
 #include <dev/psci/psci.h>
 #include <dev/psci/smccc.h>
@@ -66,6 +68,32 @@ smccc_init(void)
 	}
 }
 
+static int
+smccc_probe(device_t dev)
+{
+	int32_t version;
+
+	/*
+	 * If the version is not implemented then we treat it as SMCCC 1.0
+	 */
+	if (psci_features(SMCCC_VERSION) == PSCI_RETVAL_NOT_SUPPORTED ||
+	    (version = arm_smccc_invoke(SMCCC_VERSION, NULL)) <= 0) {
+		device_set_desc(dev, "ARM SMCCC v1.0");
+		return (0);
+	}
+
+	device_set_descf(dev, "ARM SMCCC v%d.%d", SMCCC_VERSION_MAJOR(version),
+	    SMCCC_VERSION_MINOR(version));
+
+	return (0);
+}
+
+static int
+smccc_attach(device_t dev)
+{
+	return (bus_generic_attach(dev));
+}
+
 uint32_t
 smccc_get_version(void)
 {
@@ -107,3 +135,21 @@ smccc_arch_workaround_2(int enable)
 	    ("SMCCC arch workaround 2 called with an invalid SMCCC interface"));
 	return (arm_smccc_invoke(SMCCC_ARCH_WORKAROUND_2, enable, NULL));
 }
+
+static device_method_t smccc_methods[] = {
+	DEVMETHOD(device_probe,		smccc_probe),
+	DEVMETHOD(device_attach,	smccc_attach),
+
+	DEVMETHOD(bus_add_child,	bus_generic_add_child),
+
+	DEVMETHOD_END
+};
+
+static driver_t smccc_driver = {
+	"smccc",
+	smccc_methods,
+	0,
+};
+
+EARLY_DRIVER_MODULE(smccc, psci, smccc_driver, 0, 0,
+    BUS_PASS_CPU + BUS_PASS_ORDER_FIRST);