1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2022, Linaro Limited
4 */
5
6#define LOG_CATEGORY UCLASS_RNG
7
8#include <dm.h>
9#include <linker_lists.h>
10#include <log.h>
11#include <rng.h>
12#include <dm/device_compat.h>
13#include <linux/arm-smccc.h>
14#include <linux/bitops.h>
15#include <linux/kernel.h>
16#include <linux/psci.h>
17
18#define DRIVER_NAME	"smccc-trng"
19
20/**
21 * Arm SMCCC TRNG firmware interface specification:
22 * https://developer.arm.com/documentation/den0098/latest/
23 */
24#define ARM_SMCCC_TRNG_VERSION		0x84000050
25#define ARM_SMCCC_TRNG_FEATURES		0x84000051
26#define ARM_SMCCC_TRNG_GET_UUID		0x84000052
27#define ARM_SMCCC_TRNG_RND_32		0x84000053
28#define ARM_SMCCC_TRNG_RND_64		0xC4000053
29
30#define ARM_SMCCC_RET_TRNG_SUCCESS		((ulong)0)
31#define ARM_SMCCC_RET_TRNG_NOT_SUPPORTED	((ulong)-1)
32#define ARM_SMCCC_RET_TRNG_INVALID_PARAMETER	((ulong)-2)
33#define ARM_SMCCC_RET_TRNG_NO_ENTROPY		((ulong)-3)
34
35#define TRNG_MAJOR_MASK		GENMASK(30, 16)
36#define TRNG_MAJOR_SHIFT	16
37#define TRNG_MINOR_MASK		GENMASK(15, 0)
38#define TRNG_MINOR_SHIFT	0
39
40#define TRNG_MAX_RND_64		(192 / 8)
41#define TRNG_MAX_RND_32		(96 / 8)
42
43/**
44 * struct smccc_trng_priv - Private data for SMCCC TRNG support
45 *
46 * @smc64 - True if TRNG_RND_64 is supported, false if TRNG_RND_32 is supported
47 */
48struct smccc_trng_priv {
49	bool smc64;
50};
51
52/*
53 * Copy random bytes from ulong SMCCC output register to target buffer
54 * Defines 2 function flavors for whether ARM_SMCCC_TRNG_RND_32 or
55 * ARM_SMCCC_TRNG_RND_64 was used to invoke the service.
56 */
57static size_t smc32_copy_sample(u8 **ptr, size_t size, ulong *rnd)
58{
59	size_t len = min(size, sizeof(u32));
60	u32 sample = *rnd;
61
62	memcpy(*ptr, &sample, len);
63	*ptr += len;
64
65	return size - len;
66}
67
68static size_t smc64_copy_sample(u8 **ptr, size_t size, ulong *rnd)
69{
70	size_t len = min(size, sizeof(u64));
71	u64 sample = *rnd;
72
73	memcpy(*ptr, &sample, len);
74	*ptr += len;
75
76	return size - len;
77}
78
79static int smccc_trng_read(struct udevice *dev, void *data, size_t len)
80{
81	struct psci_plat_data *smccc = dev_get_parent_plat(dev);
82	struct smccc_trng_priv *priv = dev_get_priv(dev);
83	struct arm_smccc_res res;
84	u32 func_id;
85	u8 *ptr = data;
86	size_t rem = len;
87	size_t max_sz;
88	size_t (*copy_sample)(u8 **ptr, size_t size, ulong *rnd);
89
90	if (priv->smc64) {
91		copy_sample = smc64_copy_sample;
92		func_id = ARM_SMCCC_TRNG_RND_64;
93		max_sz = TRNG_MAX_RND_64;
94	} else {
95		copy_sample = smc32_copy_sample;
96		func_id = ARM_SMCCC_TRNG_RND_32;
97		max_sz = TRNG_MAX_RND_32;
98	}
99
100	while (rem) {
101		size_t sz = min(rem, max_sz);
102
103		smccc->invoke_fn(func_id, sz * 8, 0, 0, 0, 0, 0, 0, &res);
104
105		switch (res.a0) {
106		case ARM_SMCCC_RET_TRNG_SUCCESS:
107			break;
108		case ARM_SMCCC_RET_TRNG_NO_ENTROPY:
109			continue;
110		default:
111			return -EIO;
112		}
113
114		rem -= sz;
115
116		sz = copy_sample(&ptr, sz, &res.a3);
117		if (sz)
118			sz = copy_sample(&ptr, sz, &res.a2);
119		if (sz)
120			sz = copy_sample(&ptr, sz, &res.a1);
121	}
122
123	return 0;
124}
125
126static const struct dm_rng_ops smccc_trng_ops = {
127	.read = smccc_trng_read,
128};
129
130static bool smccc_trng_is_supported(void (*invoke_fn)(unsigned long a0, unsigned long a1,
131						      unsigned long a2, unsigned long a3,
132						      unsigned long a4, unsigned long a5,
133						      unsigned long a6, unsigned long a7,
134						      struct arm_smccc_res *res))
135{
136	struct arm_smccc_res res;
137
138	(*invoke_fn)(ARM_SMCCC_ARCH_FEATURES, ARM_SMCCC_TRNG_VERSION, 0, 0, 0, 0, 0, 0, &res);
139	if (res.a0 == ARM_SMCCC_RET_NOT_SUPPORTED)
140		return false;
141
142	(*invoke_fn)(ARM_SMCCC_TRNG_VERSION, 0, 0, 0, 0, 0, 0, 0, &res);
143	if (res.a0 & BIT(31))
144		return false;
145
146	/* Test 64bit interface and fallback to 32bit interface */
147	invoke_fn(ARM_SMCCC_TRNG_FEATURES, ARM_SMCCC_TRNG_RND_64,
148		  0, 0, 0, 0, 0, 0, &res);
149
150	if (res.a0 == ARM_SMCCC_RET_TRNG_NOT_SUPPORTED)
151		invoke_fn(ARM_SMCCC_TRNG_FEATURES, ARM_SMCCC_TRNG_RND_32,
152			  0, 0, 0, 0, 0, 0, &res);
153
154	return res.a0 == ARM_SMCCC_RET_TRNG_SUCCESS;
155}
156
157ARM_SMCCC_FEATURE_DRIVER(smccc_trng) = {
158	.driver_name = DRIVER_NAME,
159	.is_supported = smccc_trng_is_supported,
160};
161
162static int smccc_trng_probe(struct udevice *dev)
163{
164	struct psci_plat_data *smccc = dev_get_parent_plat(dev);
165	struct smccc_trng_priv *priv = dev_get_priv(dev);
166	struct arm_smccc_res res;
167
168	if (!smccc || !(smccc_trng_is_supported(smccc->invoke_fn)))
169		return -ENODEV;
170
171	/* At least one of 64bit and 32bit interfaces is available */
172	smccc->invoke_fn(ARM_SMCCC_TRNG_FEATURES, ARM_SMCCC_TRNG_RND_64,
173			 0, 0, 0, 0, 0, 0, &res);
174	priv->smc64 = (res.a0 == ARM_SMCCC_RET_TRNG_SUCCESS);
175
176#ifdef DEBUG
177	smccc->invoke_fn(ARM_SMCCC_TRNG_GET_UUID, 0, 0, 0, 0, 0, 0, 0, &res);
178	if (res.a0 != ARM_SMCCC_RET_TRNG_NOT_SUPPORTED) {
179		unsigned long uuid_a0 = res.a0;
180		unsigned long uuid_a1 = res.a1;
181		unsigned long uuid_a2 = res.a2;
182		unsigned long uuid_a3 = res.a3;
183		unsigned long major, minor;
184
185		smccc->invoke_fn(ARM_SMCCC_TRNG_VERSION, 0, 0, 0, 0, 0, 0, 0, &res);
186		major = (res.a0 & TRNG_MAJOR_MASK) >> TRNG_MAJOR_SHIFT;
187		minor = (res.a0 & TRNG_MINOR_MASK) >> TRNG_MINOR_SHIFT;
188
189		dev_dbg(dev, "Version %lu.%lu, UUID %08lx-%04lx-%04lx-%04lx-%04lx%08lx\n",
190			major, minor, uuid_a0, uuid_a1 >> 16, uuid_a1 & GENMASK(16, 0),
191			uuid_a2 >> 16, uuid_a2 & GENMASK(16, 0), uuid_a3);
192	} else {
193		dev_warn(dev, "Can't get TRNG UUID\n");
194	}
195#endif
196
197	return 0;
198}
199
200U_BOOT_DRIVER(smccc_trng) = {
201	.name = DRIVER_NAME,
202	.id = UCLASS_RNG,
203	.ops = &smccc_trng_ops,
204	.probe = smccc_trng_probe,
205	.priv_auto = sizeof(struct smccc_trng_priv),
206};
207