Skip to content

Commit 38db987

Browse files
Andre-ARMwilldeacon
authored andcommitted
arm64: Add support for SMCCC TRNG entropy source
The ARM architected TRNG firmware interface, described in ARM spec DEN0098, defines an ARM SMCCC based interface to a true random number generator, provided by firmware. This can be discovered via the SMCCC >=v1.1 interface, and provides up to 192 bits of entropy per call. Hook this SMC call into arm64's arch_get_random_*() implementation, coming to the rescue when the CPU does not implement the ARM v8.5 RNG system registers. For the detection, we piggy back on the PSCI/SMCCC discovery (which gives us the conduit to use (hvc/smc)), then try to call the ARM_SMCCC_TRNG_VERSION function, which returns -1 if this interface is not implemented. Reviewed-by: Mark Brown <broonie@kernel.org> Signed-off-by: Andre Przywara <andre.przywara@arm.com> Signed-off-by: Will Deacon <will@kernel.org>
1 parent a37e31f commit 38db987

1 file changed

Lines changed: 61 additions & 11 deletions

File tree

‎arch/arm64/include/asm/archrandom.h‎

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,24 @@
44

55
#ifdef CONFIG_ARCH_RANDOM
66

7+
#include <linux/arm-smccc.h>
78
#include <linux/bug.h>
89
#include <linux/kernel.h>
910
#include <asm/cpufeature.h>
1011

12+
#define ARM_SMCCC_TRNG_MIN_VERSION 0x10000UL
13+
14+
extern bool smccc_trng_available;
15+
1116
static inline bool __init smccc_probe_trng(void)
1217
{
13-
return false;
18+
struct arm_smccc_res res;
19+
20+
arm_smccc_1_1_invoke(ARM_SMCCC_TRNG_VERSION, &res);
21+
if ((s32)res.a0 < 0)
22+
return false;
23+
24+
return res.a0 >= ARM_SMCCC_TRNG_MIN_VERSION;
1425
}
1526

1627
static inline bool __arm64_rndr(unsigned long *v)
@@ -43,26 +54,55 @@ static inline bool __must_check arch_get_random_int(unsigned int *v)
4354

4455
static inline bool __must_check arch_get_random_seed_long(unsigned long *v)
4556
{
57+
struct arm_smccc_res res;
58+
59+
/*
60+
* We prefer the SMCCC call, since its semantics (return actual
61+
* hardware backed entropy) is closer to the idea behind this
62+
* function here than what even the RNDRSS register provides
63+
* (the output of a pseudo RNG freshly seeded by a TRNG).
64+
*/
65+
if (smccc_trng_available) {
66+
arm_smccc_1_1_invoke(ARM_SMCCC_TRNG_RND64, 64, &res);
67+
if ((int)res.a0 >= 0) {
68+
*v = res.a3;
69+
return true;
70+
}
71+
}
72+
4673
/*
4774
* Only support the generic interface after we have detected
4875
* the system wide capability, avoiding complexity with the
4976
* cpufeature code and with potential scheduling between CPUs
5077
* with and without the feature.
5178
*/
52-
if (!cpus_have_const_cap(ARM64_HAS_RNG))
53-
return false;
79+
if (cpus_have_const_cap(ARM64_HAS_RNG) && __arm64_rndr(v))
80+
return true;
5481

55-
return __arm64_rndr(v);
82+
return false;
5683
}
5784

58-
5985
static inline bool __must_check arch_get_random_seed_int(unsigned int *v)
6086
{
87+
struct arm_smccc_res res;
6188
unsigned long val;
62-
bool ok = arch_get_random_seed_long(&val);
6389

64-
*v = val;
65-
return ok;
90+
if (smccc_trng_available) {
91+
arm_smccc_1_1_invoke(ARM_SMCCC_TRNG_RND64, 32, &res);
92+
if ((int)res.a0 >= 0) {
93+
*v = res.a3 & GENMASK(31, 0);
94+
return true;
95+
}
96+
}
97+
98+
if (cpus_have_const_cap(ARM64_HAS_RNG)) {
99+
if (__arm64_rndr(&val)) {
100+
*v = val;
101+
return true;
102+
}
103+
}
104+
105+
return false;
66106
}
67107

68108
static inline bool __init __early_cpu_has_rndr(void)
@@ -77,10 +117,20 @@ arch_get_random_seed_long_early(unsigned long *v)
77117
{
78118
WARN_ON(system_state != SYSTEM_BOOTING);
79119

80-
if (!__early_cpu_has_rndr())
81-
return false;
120+
if (smccc_trng_available) {
121+
struct arm_smccc_res res;
122+
123+
arm_smccc_1_1_invoke(ARM_SMCCC_TRNG_RND64, 64, &res);
124+
if ((int)res.a0 >= 0) {
125+
*v = res.a3;
126+
return true;
127+
}
128+
}
82129

83-
return __arm64_rndr(v);
130+
if (__early_cpu_has_rndr() && __arm64_rndr(v))
131+
return true;
132+
133+
return false;
84134
}
85135
#define arch_get_random_seed_long_early arch_get_random_seed_long_early
86136

0 commit comments

Comments
 (0)