[PATCH 2/9] hwrng: arm-smccc-trng - transition to the faux device interface

From: Sudeep Holla
Date: Mon Mar 17 2025 - 06:14:35 EST


The Arm SMCCC based true random number generator driver does not require
the creation of a platform device/driver. Originally, this approach was
chosen for simplicity when the driver was first implemented.

With the introduction of the lightweight faux device interface, we now
have a more appropriate alternative. Migrate the driver to utilize the
faux bus, given that the platform device it previously created was not
a real one anyway. This will simplify the code, reducing its footprint
while maintaining functionality.

Cc: Andre Przywara <andre.przywara@xxxxxxx>
Cc: Herbert Xu <herbert@xxxxxxxxxxxxxxxxxxx>
Cc: Jeff Johnson <jeff.johnson@xxxxxxxxxxxxxxxx>
Cc: linux-crypto@xxxxxxxxxxxxxxx
Signed-off-by: Sudeep Holla <sudeep.holla@xxxxxxx>
---
drivers/char/hw_random/arm_smccc_trng.c | 40 ++++++++++++++++++++++++---------
drivers/firmware/smccc/smccc.c | 21 -----------------
2 files changed, 29 insertions(+), 32 deletions(-)

diff --git a/drivers/char/hw_random/arm_smccc_trng.c b/drivers/char/hw_random/arm_smccc_trng.c
index dcb8e7f37f25c6b39f76050369b9f324b7fb2e33..2ceab17f6360baaee999a23f3d7370b7b5b7d246 100644
--- a/drivers/char/hw_random/arm_smccc_trng.c
+++ b/drivers/char/hw_random/arm_smccc_trng.c
@@ -16,9 +16,11 @@
#include <linux/device.h>
#include <linux/hw_random.h>
#include <linux/module.h>
-#include <linux/platform_device.h>
+#include <linux/device/faux.h>
#include <linux/arm-smccc.h>

+#include <asm/archrandom.h>
+
#ifdef CONFIG_ARM64
#define ARM_SMCCC_TRNG_RND ARM_SMCCC_TRNG_RND64
#define MAX_BITS_PER_CALL (3 * 64UL)
@@ -33,6 +35,8 @@
#define SMCCC_RET_TRNG_INVALID_PARAMETER -2
#define SMCCC_RET_TRNG_NO_ENTROPY -3

+bool __ro_after_init smccc_trng_available;
+
static int copy_from_registers(char *buf, struct arm_smccc_res *res,
size_t bytes)
{
@@ -94,29 +98,43 @@ static int smccc_trng_read(struct hwrng *rng, void *data, size_t max, bool wait)
return copied;
}

-static int smccc_trng_probe(struct platform_device *pdev)
+static int smccc_trng_probe(struct faux_device *fdev)
{
struct hwrng *trng;

- trng = devm_kzalloc(&pdev->dev, sizeof(*trng), GFP_KERNEL);
+ trng = devm_kzalloc(&fdev->dev, sizeof(*trng), GFP_KERNEL);
if (!trng)
return -ENOMEM;

trng->name = "smccc_trng";
trng->read = smccc_trng_read;

- return devm_hwrng_register(&pdev->dev, trng);
+ return devm_hwrng_register(&fdev->dev, trng);
}

-static struct platform_driver smccc_trng_driver = {
- .driver = {
- .name = "smccc_trng",
- },
- .probe = smccc_trng_probe,
+static struct faux_device_ops smccc_trng_ops = {
+ .probe = smccc_trng_probe,
};
-module_platform_driver(smccc_trng_driver);

-MODULE_ALIAS("platform:smccc_trng");
+static int __init smccc_trng_init(void)
+{
+ struct faux_device *fdev;
+
+ smccc_trng_available = smccc_probe_trng();
+ if (!smccc_trng_available)
+ return 0;
+
+ fdev = faux_device_create("smccc_trng", NULL, &smccc_trng_ops);
+ if (!fdev) {
+ pr_err("smccc_trng: could not create the device\n");
+ return -ENODEV;
+ }
+
+ return 0;
+}
+device_initcall(smccc_trng_init);
+
+MODULE_ALIAS("faux:smccc_trng");
MODULE_AUTHOR("Andre Przywara");
MODULE_DESCRIPTION("Arm SMCCC TRNG firmware interface support");
MODULE_LICENSE("GPL");
diff --git a/drivers/firmware/smccc/smccc.c b/drivers/firmware/smccc/smccc.c
index a74600d9f2d72a5aa0096004f53088c255927a43..0fcd175a53eeaa957d06071b3b26f4c3a3c7116e 100644
--- a/drivers/firmware/smccc/smccc.c
+++ b/drivers/firmware/smccc/smccc.c
@@ -9,13 +9,10 @@
#include <linux/init.h>
#include <linux/arm-smccc.h>
#include <linux/kernel.h>
-#include <linux/platform_device.h>
-#include <asm/archrandom.h>

static u32 smccc_version = ARM_SMCCC_VERSION_1_0;
static enum arm_smccc_conduit smccc_conduit = SMCCC_CONDUIT_NONE;

-bool __ro_after_init smccc_trng_available = false;
s32 __ro_after_init smccc_soc_id_version = SMCCC_RET_NOT_SUPPORTED;
s32 __ro_after_init smccc_soc_id_revision = SMCCC_RET_NOT_SUPPORTED;

@@ -26,8 +23,6 @@ void __init arm_smccc_version_init(u32 version, enum arm_smccc_conduit conduit)
smccc_version = version;
smccc_conduit = conduit;

- smccc_trng_available = smccc_probe_trng();
-
if ((smccc_version >= ARM_SMCCC_VERSION_1_2) &&
(smccc_conduit != SMCCC_CONDUIT_NONE)) {
arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
@@ -66,19 +61,3 @@ s32 arm_smccc_get_soc_id_revision(void)
return smccc_soc_id_revision;
}
EXPORT_SYMBOL_GPL(arm_smccc_get_soc_id_revision);
-
-static int __init smccc_devices_init(void)
-{
- struct platform_device *pdev;
-
- if (smccc_trng_available) {
- pdev = platform_device_register_simple("smccc_trng", -1,
- NULL, 0);
- if (IS_ERR(pdev))
- pr_err("smccc_trng: could not register device: %ld\n",
- PTR_ERR(pdev));
- }
-
- return 0;
-}
-device_initcall(smccc_devices_init);

--
2.34.1