1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2022, Linaro Limited
6 #define LOG_CATEGORY UCLASS_RNG
10 #include <linker_lists.h>
13 #include <dm/device_compat.h>
14 #include <linux/arm-smccc.h>
15 #include <linux/bitops.h>
16 #include <linux/kernel.h>
17 #include <linux/psci.h>
19 #define DRIVER_NAME "smccc-trng"
22 * Arm SMCCC TRNG firmware interface specification:
23 * https://developer.arm.com/documentation/den0098/latest/
25 #define ARM_SMCCC_TRNG_VERSION 0x84000050
26 #define ARM_SMCCC_TRNG_FEATURES 0x84000051
27 #define ARM_SMCCC_TRNG_GET_UUID 0x84000052
28 #define ARM_SMCCC_TRNG_RND_32 0x84000053
29 #define ARM_SMCCC_TRNG_RND_64 0xC4000053
31 #define ARM_SMCCC_RET_TRNG_SUCCESS ((ulong)0)
32 #define ARM_SMCCC_RET_TRNG_NOT_SUPPORTED ((ulong)-1)
33 #define ARM_SMCCC_RET_TRNG_INVALID_PARAMETER ((ulong)-2)
34 #define ARM_SMCCC_RET_TRNG_NO_ENTROPY ((ulong)-3)
36 #define TRNG_MAJOR_MASK GENMASK(30, 16)
37 #define TRNG_MAJOR_SHIFT 16
38 #define TRNG_MINOR_MASK GENMASK(15, 0)
39 #define TRNG_MINOR_SHIFT 0
41 #define TRNG_MAX_RND_64 (192 / 8)
42 #define TRNG_MAX_RND_32 (96 / 8)
45 * struct smccc_trng_priv - Private data for SMCCC TRNG support
47 * @smc64 - True if TRNG_RND_64 is supported, false if TRNG_RND_32 is supported
49 struct smccc_trng_priv {
54 * Copy random bytes from ulong SMCCC output register to target buffer
55 * Defines 2 function flavors for whether ARM_SMCCC_TRNG_RND_32 or
56 * ARM_SMCCC_TRNG_RND_64 was used to invoke the service.
58 static size_t smc32_copy_sample(u8 **ptr, size_t size, ulong *rnd)
60 size_t len = min(size, sizeof(u32));
63 memcpy(*ptr, &sample, len);
69 static size_t smc64_copy_sample(u8 **ptr, size_t size, ulong *rnd)
71 size_t len = min(size, sizeof(u64));
74 memcpy(*ptr, &sample, len);
80 static int smccc_trng_read(struct udevice *dev, void *data, size_t len)
82 struct psci_plat_data *smccc = dev_get_parent_plat(dev);
83 struct smccc_trng_priv *priv = dev_get_priv(dev);
84 struct arm_smccc_res res;
89 size_t (*copy_sample)(u8 **ptr, size_t size, ulong *rnd);
92 copy_sample = smc64_copy_sample;
93 func_id = ARM_SMCCC_TRNG_RND_64;
94 max_sz = TRNG_MAX_RND_64;
96 copy_sample = smc32_copy_sample;
97 func_id = ARM_SMCCC_TRNG_RND_32;
98 max_sz = TRNG_MAX_RND_32;
102 size_t sz = min(rem, max_sz);
104 smccc->invoke_fn(func_id, sz * 8, 0, 0, 0, 0, 0, 0, &res);
107 case ARM_SMCCC_RET_TRNG_SUCCESS:
109 case ARM_SMCCC_RET_TRNG_NO_ENTROPY:
117 sz = copy_sample(&ptr, sz, &res.a3);
119 sz = copy_sample(&ptr, sz, &res.a2);
121 sz = copy_sample(&ptr, sz, &res.a1);
127 static const struct dm_rng_ops smccc_trng_ops = {
128 .read = smccc_trng_read,
131 static bool smccc_trng_is_supported(void (*invoke_fn)(unsigned long a0, unsigned long a1,
132 unsigned long a2, unsigned long a3,
133 unsigned long a4, unsigned long a5,
134 unsigned long a6, unsigned long a7,
135 struct arm_smccc_res *res))
137 struct arm_smccc_res res;
139 (*invoke_fn)(ARM_SMCCC_ARCH_FEATURES, ARM_SMCCC_TRNG_VERSION, 0, 0, 0, 0, 0, 0, &res);
140 if (res.a0 == ARM_SMCCC_RET_NOT_SUPPORTED)
143 (*invoke_fn)(ARM_SMCCC_TRNG_VERSION, 0, 0, 0, 0, 0, 0, 0, &res);
144 if (res.a0 & BIT(31))
147 /* Test 64bit interface and fallback to 32bit interface */
148 invoke_fn(ARM_SMCCC_TRNG_FEATURES, ARM_SMCCC_TRNG_RND_64,
149 0, 0, 0, 0, 0, 0, &res);
151 if (res.a0 == ARM_SMCCC_RET_TRNG_NOT_SUPPORTED)
152 invoke_fn(ARM_SMCCC_TRNG_FEATURES, ARM_SMCCC_TRNG_RND_32,
153 0, 0, 0, 0, 0, 0, &res);
155 return res.a0 == ARM_SMCCC_RET_TRNG_SUCCESS;
158 ARM_SMCCC_FEATURE_DRIVER(smccc_trng) = {
159 .driver_name = DRIVER_NAME,
160 .is_supported = smccc_trng_is_supported,
163 static int smccc_trng_probe(struct udevice *dev)
165 struct psci_plat_data *smccc = dev_get_parent_plat(dev);
166 struct smccc_trng_priv *priv = dev_get_priv(dev);
167 struct arm_smccc_res res;
169 if (!(smccc_trng_is_supported(smccc->invoke_fn)))
172 /* At least one of 64bit and 32bit interfaces is available */
173 smccc->invoke_fn(ARM_SMCCC_TRNG_FEATURES, ARM_SMCCC_TRNG_RND_64,
174 0, 0, 0, 0, 0, 0, &res);
175 priv->smc64 = (res.a0 == ARM_SMCCC_RET_TRNG_SUCCESS);
178 smccc->invoke_fn(ARM_SMCCC_TRNG_GET_UUID, 0, 0, 0, 0, 0, 0, 0, &res);
179 if (res.a0 != ARM_SMCCC_RET_TRNG_NOT_SUPPORTED) {
180 unsigned long uuid_a0 = res.a0;
181 unsigned long uuid_a1 = res.a1;
182 unsigned long uuid_a2 = res.a2;
183 unsigned long uuid_a3 = res.a3;
184 unsigned long major, minor;
186 smccc->invoke_fn(ARM_SMCCC_TRNG_VERSION, 0, 0, 0, 0, 0, 0, 0, &res);
187 major = (res.a0 & TRNG_MAJOR_MASK) >> TRNG_MAJOR_SHIFT;
188 minor = (res.a0 & TRNG_MINOR_MASK) >> TRNG_MINOR_SHIFT;
190 dev_dbg(dev, "Version %lu.%lu, UUID %08lx-%04lx-%04lx-%04lx-%04lx%08lx\n",
191 major, minor, uuid_a0, uuid_a1 >> 16, uuid_a1 & GENMASK(16, 0),
192 uuid_a2 >> 16, uuid_a2 & GENMASK(16, 0), uuid_a3);
194 dev_warn(dev, "Can't get TRNG UUID\n");
201 U_BOOT_DRIVER(smccc_trng) = {
204 .ops = &smccc_trng_ops,
205 .probe = smccc_trng_probe,
206 .priv_auto = sizeof(struct smccc_trng_priv),