From badef4cd4a9154776238cb1fad6d22bc93340303 Mon Sep 17 00:00:00 2001 From: Heinrich Schuchardt Date: Sat, 4 Nov 2023 09:00:07 +0200 Subject: [PATCH 1/2] rng: fix ARMv8.5 RNDR driver In different parts of our code we assume that the first RNG device is the one to be used. Therefore it is preferable to detect the availability of the RNDR register already in the bind method. For signaling the non-existence of a device the driver model requires using ENOENT (and not ENODEV). Fixes: 31565bb0aa2d ("driver: rng: Add DM_RNG interface for ARMv8.5 RNDR registers") Signed-off-by: Heinrich Schuchardt Reviewed-by: Andre Przywara Tested-by: Andre Przywara --- drivers/rng/arm_rndr.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/rng/arm_rndr.c b/drivers/rng/arm_rndr.c index 55989743eae..4512330e68d 100644 --- a/drivers/rng/arm_rndr.c +++ b/drivers/rng/arm_rndr.c @@ -62,10 +62,10 @@ static const struct dm_rng_ops arm_rndr_ops = { .read = arm_rndr_read, }; -static int arm_rndr_probe(struct udevice *dev) +static int arm_rndr_bind(struct udevice *dev) { if (!cpu_has_rndr()) - return -ENODEV; + return -ENOENT; return 0; } @@ -74,7 +74,7 @@ U_BOOT_DRIVER(arm_rndr) = { .name = DRIVER_NAME, .id = UCLASS_RNG, .ops = &arm_rndr_ops, - .probe = arm_rndr_probe, + .bind = arm_rndr_bind, }; U_BOOT_DRVINFO(cpu_arm_rndr) = { From 1351cd3b4b1b18cafa4893a44378ca6b1d091c8e Mon Sep 17 00:00:00 2001 From: Heinrich Schuchardt Date: Sat, 4 Nov 2023 08:51:07 +0200 Subject: [PATCH 2/2] rng: detect RISC-V Zkr RNG device in bind method The existence of devices should be checked in the bind method and not in the probe method. Adjust the RISC-V Zkr RNG driver accordingly. Use ENOENT (and not ENODEV) to signal that the device is not available. Fixes: ceec977ba1a9 ("rng: Provide a RNG based on the RISC-V Zkr ISA extension") Reported-by: Andre Przywara Signed-off-by: Heinrich Schuchardt --- drivers/rng/riscv_zkr_rng.c | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/drivers/rng/riscv_zkr_rng.c b/drivers/rng/riscv_zkr_rng.c index 8c9e111e2e0..48a5251988f 100644 --- a/drivers/rng/riscv_zkr_rng.c +++ b/drivers/rng/riscv_zkr_rng.c @@ -55,7 +55,7 @@ static int riscv_zkr_read(struct udevice *dev, void *data, size_t len) } break; case DEAD: - return -ENODEV; + return -ENOENT; } } @@ -63,16 +63,16 @@ static int riscv_zkr_read(struct udevice *dev, void *data, size_t len) } /** - * riscv_zkr_probe() - check if the seed register is available + * riscv_zkr_bind() - check if the seed register is available * - * If the SBI software has not set mseccfg.sseed=1 or the Zkr - * extension is not available this probe function will result - * in an exception. Currently we cannot recover from this. + * If the SBI software has not set mseccfg.sseed=1 or the Zkr extension is not + * available, reading the seed register will result in an exception from which + * this function safely resumes. * * @dev: RNG device * Return: 0 if successfully probed */ -static int riscv_zkr_probe(struct udevice *dev) +static int riscv_zkr_bind(struct udevice *dev) { struct resume_data resume; int ret; @@ -87,7 +87,24 @@ static int riscv_zkr_probe(struct udevice *dev) val = read_seed(); set_resume(NULL); if (ret) - return -ENODEV; + return -ENOENT; + + return 0; +} + +/** + * riscv_zkr_probe() - check if entropy is available + * + * The bind method already checked that the seed register can be read without + * excpetiong. Here we wait for the self test to finish and entropy becoming + * available. + * + * @dev: RNG device + * Return: 0 if successfully probed + */ +static int riscv_zkr_probe(struct udevice *dev) +{ + u32 val; do { val = read_seed(); @@ -95,7 +112,7 @@ static int riscv_zkr_probe(struct udevice *dev) } while (val == BIST || val == WAIT); if (val == DEAD) - return -ENODEV; + return -ENOENT; return 0; } @@ -108,6 +125,7 @@ U_BOOT_DRIVER(riscv_zkr) = { .name = DRIVER_NAME, .id = UCLASS_RNG, .ops = &riscv_zkr_ops, + .bind = riscv_zkr_bind, .probe = riscv_zkr_probe, };