Skip to content

Commit 9dc8313

Browse files
committed
Merge branch 'for-next/rng' into for-next/core
Add support for the TRNG firmware call introduced by Arm spec DEN0098. * for-next/rng: arm64: Add support for SMCCC TRNG entropy source firmware: smccc: Introduce SMCCC TRNG framework firmware: smccc: Add SMCCC TRNG function call IDs
2 parents cf6a85a + 38db987 commit 9dc8313

4 files changed

Lines changed: 119 additions & 10 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef _ASM_ARCHRANDOM_H
3+
#define _ASM_ARCHRANDOM_H
4+
5+
static inline bool __init smccc_probe_trng(void)
6+
{
7+
return false;
8+
}
9+
10+
#endif /* _ASM_ARCHRANDOM_H */

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

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,26 @@
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+
16+
static inline bool __init smccc_probe_trng(void)
17+
{
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;
25+
}
26+
1127
static inline bool __arm64_rndr(unsigned long *v)
1228
{
1329
bool ok;
@@ -38,26 +54,55 @@ static inline bool __must_check arch_get_random_int(unsigned int *v)
3854

3955
static inline bool __must_check arch_get_random_seed_long(unsigned long *v)
4056
{
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+
4173
/*
4274
* Only support the generic interface after we have detected
4375
* the system wide capability, avoiding complexity with the
4476
* cpufeature code and with potential scheduling between CPUs
4577
* with and without the feature.
4678
*/
47-
if (!cpus_have_const_cap(ARM64_HAS_RNG))
48-
return false;
79+
if (cpus_have_const_cap(ARM64_HAS_RNG) && __arm64_rndr(v))
80+
return true;
4981

50-
return __arm64_rndr(v);
82+
return false;
5183
}
5284

53-
5485
static inline bool __must_check arch_get_random_seed_int(unsigned int *v)
5586
{
87+
struct arm_smccc_res res;
5688
unsigned long val;
57-
bool ok = arch_get_random_seed_long(&val);
5889

59-
*v = val;
60-
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;
61106
}
62107

63108
static inline bool __init __early_cpu_has_rndr(void)
@@ -72,12 +117,29 @@ arch_get_random_seed_long_early(unsigned long *v)
72117
{
73118
WARN_ON(system_state != SYSTEM_BOOTING);
74119

75-
if (!__early_cpu_has_rndr())
76-
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+
}
129+
130+
if (__early_cpu_has_rndr() && __arm64_rndr(v))
131+
return true;
77132

78-
return __arm64_rndr(v);
133+
return false;
79134
}
80135
#define arch_get_random_seed_long_early arch_get_random_seed_long_early
81136

137+
#else /* !CONFIG_ARCH_RANDOM */
138+
139+
static inline bool __init smccc_probe_trng(void)
140+
{
141+
return false;
142+
}
143+
82144
#endif /* CONFIG_ARCH_RANDOM */
83145
#endif /* _ASM_ARCHRANDOM_H */

‎drivers/firmware/smccc/smccc.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,22 @@
55

66
#define pr_fmt(fmt) "smccc: " fmt
77

8+
#include <linux/cache.h>
89
#include <linux/init.h>
910
#include <linux/arm-smccc.h>
11+
#include <asm/archrandom.h>
1012

1113
static u32 smccc_version = ARM_SMCCC_VERSION_1_0;
1214
static enum arm_smccc_conduit smccc_conduit = SMCCC_CONDUIT_NONE;
1315

16+
bool __ro_after_init smccc_trng_available = false;
17+
1418
void __init arm_smccc_version_init(u32 version, enum arm_smccc_conduit conduit)
1519
{
1620
smccc_version = version;
1721
smccc_conduit = conduit;
22+
23+
smccc_trng_available = smccc_probe_trng();
1824
}
1925

2026
enum arm_smccc_conduit arm_smccc_1_1_get_conduit(void)

‎include/linux/arm-smccc.h‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,37 @@
102102
ARM_SMCCC_OWNER_STANDARD_HYP, \
103103
0x21)
104104

105+
/* TRNG entropy source calls (defined by ARM DEN0098) */
106+
#define ARM_SMCCC_TRNG_VERSION \
107+
ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \
108+
ARM_SMCCC_SMC_32, \
109+
ARM_SMCCC_OWNER_STANDARD, \
110+
0x50)
111+
112+
#define ARM_SMCCC_TRNG_FEATURES \
113+
ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \
114+
ARM_SMCCC_SMC_32, \
115+
ARM_SMCCC_OWNER_STANDARD, \
116+
0x51)
117+
118+
#define ARM_SMCCC_TRNG_GET_UUID \
119+
ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \
120+
ARM_SMCCC_SMC_32, \
121+
ARM_SMCCC_OWNER_STANDARD, \
122+
0x52)
123+
124+
#define ARM_SMCCC_TRNG_RND32 \
125+
ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \
126+
ARM_SMCCC_SMC_32, \
127+
ARM_SMCCC_OWNER_STANDARD, \
128+
0x53)
129+
130+
#define ARM_SMCCC_TRNG_RND64 \
131+
ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \
132+
ARM_SMCCC_SMC_64, \
133+
ARM_SMCCC_OWNER_STANDARD, \
134+
0x53)
135+
105136
/*
106137
* Return codes defined in ARM DEN 0070A
107138
* ARM DEN 0070A is now merged/consolidated into ARM DEN 0028 C

0 commit comments

Comments
 (0)