xref: /optee_os/core/drivers/stm32_rng.c (revision ea8ba29599c9df36b98069909025171b0ba5a238)
1f3c22059SEtienne Carriere // SPDX-License-Identifier: BSD-3-Clause
2f3c22059SEtienne Carriere /*
3*ea8ba295SGatien Chevallier  * Copyright (c) 2018-2023, STMicroelectronics
4f3c22059SEtienne Carriere  */
5f3c22059SEtienne Carriere 
6f3c22059SEtienne Carriere #include <assert.h>
7d7a1a7d2SEtienne Carriere #include <drivers/clk.h>
8d7a1a7d2SEtienne Carriere #include <drivers/clk_dt.h>
9*ea8ba295SGatien Chevallier #include <drivers/rstctrl.h>
10f3c22059SEtienne Carriere #include <drivers/stm32_rng.h>
11f3c22059SEtienne Carriere #include <io.h>
12f3c22059SEtienne Carriere #include <kernel/delay.h>
13a2fc83d1SJerome Forissier #include <kernel/dt.h>
14*ea8ba295SGatien Chevallier #include <kernel/dt_driver.h>
1565401337SJens Wiklander #include <kernel/boot.h>
16f3c22059SEtienne Carriere #include <kernel/panic.h>
1765b5ada4SMarouene Boubakri #include <kernel/thread.h>
18a2fc83d1SJerome Forissier #include <libfdt.h>
19f3c22059SEtienne Carriere #include <mm/core_memprot.h>
20097f329aSEtienne Carriere #include <rng_support.h>
21f3c22059SEtienne Carriere #include <stdbool.h>
22f3c22059SEtienne Carriere #include <stm32_util.h>
23f3c22059SEtienne Carriere #include <string.h>
24cd451498SEtienne Carriere #include <tee/tee_cryp_utl.h>
25f3c22059SEtienne Carriere 
26f3c22059SEtienne Carriere #define RNG_CR			0x00U
27f3c22059SEtienne Carriere #define RNG_SR			0x04U
28f3c22059SEtienne Carriere #define RNG_DR			0x08U
29f3c22059SEtienne Carriere 
30f3c22059SEtienne Carriere #define RNG_CR_RNGEN		BIT(2)
31f3c22059SEtienne Carriere #define RNG_CR_IE		BIT(3)
32f3c22059SEtienne Carriere #define RNG_CR_CED		BIT(5)
33f3c22059SEtienne Carriere 
34f3c22059SEtienne Carriere #define RNG_SR_DRDY		BIT(0)
35f3c22059SEtienne Carriere #define RNG_SR_CECS		BIT(1)
36f3c22059SEtienne Carriere #define RNG_SR_SECS		BIT(2)
37f3c22059SEtienne Carriere #define RNG_SR_CEIS		BIT(5)
38f3c22059SEtienne Carriere #define RNG_SR_SEIS		BIT(6)
39f3c22059SEtienne Carriere 
40c99311c8SEtienne Carriere #define RNG_TIMEOUT_US		U(100000)
41*ea8ba295SGatien Chevallier #define RNG_RESET_TIMEOUT_US	U(1000)
42f3c22059SEtienne Carriere 
43f3c22059SEtienne Carriere struct stm32_rng_instance {
44f3c22059SEtienne Carriere 	struct io_pa_va base;
45d7a1a7d2SEtienne Carriere 	struct clk *clock;
46*ea8ba295SGatien Chevallier 	struct rstctrl *rstctrl;
47f3c22059SEtienne Carriere 	unsigned int lock;
48f3c22059SEtienne Carriere 	unsigned int refcount;
49d8682c4cSEtienne Carriere 	bool release_post_boot;
50f3c22059SEtienne Carriere };
51f3c22059SEtienne Carriere 
52*ea8ba295SGatien Chevallier /* Expect at most a single RNG instance */
53f3c22059SEtienne Carriere static struct stm32_rng_instance *stm32_rng;
54f3c22059SEtienne Carriere 
55f3c22059SEtienne Carriere /*
56f3c22059SEtienne Carriere  * Extracts from the STM32 RNG specification:
57f3c22059SEtienne Carriere  *
58f3c22059SEtienne Carriere  * When a noise source (or seed) error occurs, the RNG stops generating
59f3c22059SEtienne Carriere  * random numbers and sets to “1” both SEIS and SECS bits to indicate
60f3c22059SEtienne Carriere  * that a seed error occurred. (...)
61f3c22059SEtienne Carriere 
62f3c22059SEtienne Carriere  * The following sequence shall be used to fully recover from a seed
63f3c22059SEtienne Carriere  * error after the RNG initialization:
64f3c22059SEtienne Carriere  * 1. Clear the SEIS bit by writing it to “0”.
65f3c22059SEtienne Carriere  * 2. Read out 12 words from the RNG_DR register, and discard each of
66f3c22059SEtienne Carriere  * them in order to clean the pipeline.
67f3c22059SEtienne Carriere  * 3. Confirm that SEIS is still cleared. Random number generation is
68f3c22059SEtienne Carriere  * back to normal.
69f3c22059SEtienne Carriere  */
70f3c22059SEtienne Carriere static void conceal_seed_error(vaddr_t rng_base)
71f3c22059SEtienne Carriere {
72f3c22059SEtienne Carriere 	if (io_read32(rng_base + RNG_SR) & (RNG_SR_SECS | RNG_SR_SEIS)) {
73f3c22059SEtienne Carriere 		size_t i = 0;
74f3c22059SEtienne Carriere 
75f3c22059SEtienne Carriere 		io_mask32(rng_base + RNG_SR, 0, RNG_SR_SEIS);
76f3c22059SEtienne Carriere 
77f3c22059SEtienne Carriere 		for (i = 12; i != 0; i--)
78f3c22059SEtienne Carriere 			(void)io_read32(rng_base + RNG_DR);
79f3c22059SEtienne Carriere 
80f3c22059SEtienne Carriere 		if (io_read32(rng_base + RNG_SR) & RNG_SR_SEIS)
81f3c22059SEtienne Carriere 			panic("RNG noise");
82f3c22059SEtienne Carriere 	}
83f3c22059SEtienne Carriere }
84f3c22059SEtienne Carriere 
85f3c22059SEtienne Carriere #define RNG_FIFO_BYTE_DEPTH		16u
86f3c22059SEtienne Carriere 
87c99311c8SEtienne Carriere static TEE_Result read_available(vaddr_t rng_base, uint8_t *out, size_t *size)
88f3c22059SEtienne Carriere {
89c99311c8SEtienne Carriere 	uint8_t *buf = NULL;
90c99311c8SEtienne Carriere 	size_t req_size = 0;
91c99311c8SEtienne Carriere 	size_t len = 0;
92f3c22059SEtienne Carriere 
93f3c22059SEtienne Carriere 	conceal_seed_error(rng_base);
94f3c22059SEtienne Carriere 
9523123473SEtienne Carriere 	if (!(io_read32(rng_base + RNG_SR) & RNG_SR_DRDY)) {
9623123473SEtienne Carriere 		FMSG("RNG not ready");
97c99311c8SEtienne Carriere 		return TEE_ERROR_NO_DATA;
9823123473SEtienne Carriere 	}
99f3c22059SEtienne Carriere 
10023123473SEtienne Carriere 	if (io_read32(rng_base + RNG_SR) & RNG_SR_SEIS) {
10123123473SEtienne Carriere 		FMSG("RNG noise error");
102c99311c8SEtienne Carriere 		return TEE_ERROR_NO_DATA;
10323123473SEtienne Carriere 	}
104c99311c8SEtienne Carriere 
105c99311c8SEtienne Carriere 	buf = out;
106c99311c8SEtienne Carriere 	req_size = MIN(RNG_FIFO_BYTE_DEPTH, *size);
107c99311c8SEtienne Carriere 	len = req_size;
108f3c22059SEtienne Carriere 
109f3c22059SEtienne Carriere 	/* RNG is ready: read up to 4 32bit words */
110f3c22059SEtienne Carriere 	while (len) {
111f3c22059SEtienne Carriere 		uint32_t data32 = io_read32(rng_base + RNG_DR);
112f3c22059SEtienne Carriere 		size_t sz = MIN(len, sizeof(uint32_t));
113f3c22059SEtienne Carriere 
114f3c22059SEtienne Carriere 		memcpy(buf, &data32, sz);
115f3c22059SEtienne Carriere 		buf += sz;
116f3c22059SEtienne Carriere 		len -= sz;
117f3c22059SEtienne Carriere 	}
118c99311c8SEtienne Carriere 
119f3c22059SEtienne Carriere 	*size = req_size;
120f3c22059SEtienne Carriere 
121c99311c8SEtienne Carriere 	return TEE_SUCCESS;
122f3c22059SEtienne Carriere }
123f3c22059SEtienne Carriere 
124f3c22059SEtienne Carriere static void gate_rng(bool enable, struct stm32_rng_instance *dev)
125f3c22059SEtienne Carriere {
126c2e4eb43SAnton Rybakov 	vaddr_t rng_cr = io_pa_or_va(&dev->base, 1) + RNG_CR;
127f3c22059SEtienne Carriere 	uint32_t exceptions = may_spin_lock(&dev->lock);
128f3c22059SEtienne Carriere 
129f3c22059SEtienne Carriere 	if (enable) {
130f3c22059SEtienne Carriere 		/* incr_refcnt return non zero if resource shall be enabled */
131f3c22059SEtienne Carriere 		if (incr_refcnt(&dev->refcount)) {
13223123473SEtienne Carriere 			FMSG("enable RNG");
133d7a1a7d2SEtienne Carriere 			clk_enable(dev->clock);
134f3c22059SEtienne Carriere 			io_write32(rng_cr, 0);
135f3c22059SEtienne Carriere 			io_write32(rng_cr, RNG_CR_RNGEN | RNG_CR_CED);
136f3c22059SEtienne Carriere 		}
137f3c22059SEtienne Carriere 	} else {
138f3c22059SEtienne Carriere 		/* decr_refcnt return non zero if resource shall be disabled */
139f3c22059SEtienne Carriere 		if (decr_refcnt(&dev->refcount)) {
14023123473SEtienne Carriere 			FMSG("disable RNG");
141f3c22059SEtienne Carriere 			io_write32(rng_cr, 0);
142d7a1a7d2SEtienne Carriere 			clk_disable(dev->clock);
143f3c22059SEtienne Carriere 		}
144f3c22059SEtienne Carriere 	}
145f3c22059SEtienne Carriere 
146f3c22059SEtienne Carriere 	may_spin_unlock(&dev->lock, exceptions);
147f3c22059SEtienne Carriere }
148f3c22059SEtienne Carriere 
149f3c22059SEtienne Carriere TEE_Result stm32_rng_read(uint8_t *out, size_t size)
150f3c22059SEtienne Carriere {
151c99311c8SEtienne Carriere 	TEE_Result rc = TEE_ERROR_GENERIC;
152c99311c8SEtienne Carriere 	bool burst_timeout = false;
153c99311c8SEtienne Carriere 	uint64_t timeout_ref = 0;
154f3c22059SEtienne Carriere 	uint32_t exceptions = 0;
155f3c22059SEtienne Carriere 	uint8_t *out_ptr = out;
156c99311c8SEtienne Carriere 	vaddr_t rng_base = 0;
157f3c22059SEtienne Carriere 	size_t out_size = 0;
158f3c22059SEtienne Carriere 
159f3c22059SEtienne Carriere 	if (!stm32_rng) {
160f3c22059SEtienne Carriere 		DMSG("No RNG");
161f3c22059SEtienne Carriere 		return TEE_ERROR_NOT_SUPPORTED;
162f3c22059SEtienne Carriere 	}
163f3c22059SEtienne Carriere 
164f3c22059SEtienne Carriere 	gate_rng(true, stm32_rng);
165c99311c8SEtienne Carriere 	rng_base = io_pa_or_va(&stm32_rng->base, 1);
166c99311c8SEtienne Carriere 
167c99311c8SEtienne Carriere 	/* Arm timeout */
168c99311c8SEtienne Carriere 	timeout_ref = timeout_init_us(RNG_TIMEOUT_US);
169c99311c8SEtienne Carriere 	burst_timeout = false;
170f3c22059SEtienne Carriere 
171f3c22059SEtienne Carriere 	while (out_size < size) {
172f3c22059SEtienne Carriere 		/* Read by chunks of the size the RNG FIFO depth */
173f3c22059SEtienne Carriere 		size_t sz = size - out_size;
174f3c22059SEtienne Carriere 
175f3c22059SEtienne Carriere 		exceptions = may_spin_lock(&stm32_rng->lock);
176f3c22059SEtienne Carriere 
177c99311c8SEtienne Carriere 		rc = read_available(rng_base, out_ptr, &sz);
178c99311c8SEtienne Carriere 
179c99311c8SEtienne Carriere 		/* Raise timeout only if we failed to get some samples */
180c99311c8SEtienne Carriere 		assert(!rc || rc == TEE_ERROR_NO_DATA);
181c99311c8SEtienne Carriere 		if (rc)
182c99311c8SEtienne Carriere 			burst_timeout = timeout_elapsed(timeout_ref);
183f3c22059SEtienne Carriere 
184f3c22059SEtienne Carriere 		may_spin_unlock(&stm32_rng->lock, exceptions);
185f3c22059SEtienne Carriere 
186c99311c8SEtienne Carriere 		if (burst_timeout) {
187c99311c8SEtienne Carriere 			rc = TEE_ERROR_GENERIC;
188c99311c8SEtienne Carriere 			goto out;
189f3c22059SEtienne Carriere 		}
190f3c22059SEtienne Carriere 
191c99311c8SEtienne Carriere 		if (!rc) {
192c99311c8SEtienne Carriere 			out_size += sz;
193c99311c8SEtienne Carriere 			out_ptr += sz;
194c99311c8SEtienne Carriere 			/* Re-arm timeout */
195c99311c8SEtienne Carriere 			timeout_ref = timeout_init_us(RNG_TIMEOUT_US);
196c99311c8SEtienne Carriere 			burst_timeout = false;
197c99311c8SEtienne Carriere 		}
198c99311c8SEtienne Carriere 	}
199c99311c8SEtienne Carriere 
200c99311c8SEtienne Carriere out:
201c99311c8SEtienne Carriere 	assert(!rc || rc == TEE_ERROR_GENERIC);
202f3c22059SEtienne Carriere 	gate_rng(false, stm32_rng);
203f3c22059SEtienne Carriere 
204f3c22059SEtienne Carriere 	return rc;
205f3c22059SEtienne Carriere }
206f3c22059SEtienne Carriere 
207cd451498SEtienne Carriere #ifdef CFG_WITH_SOFTWARE_PRNG
208cd451498SEtienne Carriere /* Override weak plat_rng_init with platform handler to seed PRNG */
209cd451498SEtienne Carriere void plat_rng_init(void)
210cd451498SEtienne Carriere {
211cd451498SEtienne Carriere 	uint8_t seed[RNG_FIFO_BYTE_DEPTH] = { };
212cd451498SEtienne Carriere 
213cd451498SEtienne Carriere 	if (stm32_rng_read(seed, sizeof(seed)))
214cd451498SEtienne Carriere 		panic();
215cd451498SEtienne Carriere 
216cd451498SEtienne Carriere 	if (crypto_rng_init(seed, sizeof(seed)))
217cd451498SEtienne Carriere 		panic();
218cd451498SEtienne Carriere 
219cd451498SEtienne Carriere 	DMSG("PRNG seeded with RNG");
220cd451498SEtienne Carriere }
221cd451498SEtienne Carriere #else
222cb2478efSAndrew Davis TEE_Result hw_get_random_bytes(void *out, size_t size)
223097f329aSEtienne Carriere {
224097f329aSEtienne Carriere 	return stm32_rng_read(out, size);
225097f329aSEtienne Carriere }
226097f329aSEtienne Carriere #endif
227097f329aSEtienne Carriere 
228f3c22059SEtienne Carriere #ifdef CFG_EMBED_DTB
229*ea8ba295SGatien Chevallier static TEE_Result stm32_rng_parse_fdt(const void *fdt, int node)
230f3c22059SEtienne Carriere {
231d7a1a7d2SEtienne Carriere 	TEE_Result res = TEE_ERROR_GENERIC;
232*ea8ba295SGatien Chevallier 	struct dt_node_info dt_rng = { };
233f3c22059SEtienne Carriere 
234*ea8ba295SGatien Chevallier 	_fdt_fill_device_info(fdt, &dt_rng, node);
235*ea8ba295SGatien Chevallier 	if (dt_rng.reg == DT_INFO_INVALID_REG)
236*ea8ba295SGatien Chevallier 		return TEE_ERROR_BAD_PARAMETERS;
237f3c22059SEtienne Carriere 
238*ea8ba295SGatien Chevallier 	stm32_rng->base.pa = dt_rng.reg;
239*ea8ba295SGatien Chevallier 	stm32_rng->base.va = io_pa_or_va_secure(&stm32_rng->base,
240*ea8ba295SGatien Chevallier 						dt_rng.reg_size);
241*ea8ba295SGatien Chevallier 	assert(stm32_rng->base.va);
242f3c22059SEtienne Carriere 
243*ea8ba295SGatien Chevallier 	res = rstctrl_dt_get_by_index(fdt, node, 0, &stm32_rng->rstctrl);
244*ea8ba295SGatien Chevallier 	if (res != TEE_SUCCESS && res != TEE_ERROR_ITEM_NOT_FOUND)
245*ea8ba295SGatien Chevallier 		return res;
24668c4a16bSEtienne Carriere 
247d7a1a7d2SEtienne Carriere 	res = clk_dt_get_by_index(fdt, node, 0, &stm32_rng->clock);
248d7a1a7d2SEtienne Carriere 	if (res)
249d7a1a7d2SEtienne Carriere 		return res;
250d7a1a7d2SEtienne Carriere 
251*ea8ba295SGatien Chevallier 	/* Release device if not used at runtime or for pm transitions */
252*ea8ba295SGatien Chevallier 	stm32_rng->release_post_boot = IS_ENABLED(CFG_WITH_SOFTWARE_PRNG) &&
253*ea8ba295SGatien Chevallier 				       !IS_ENABLED(CFG_PM);
254f3c22059SEtienne Carriere 
255f3c22059SEtienne Carriere 	return TEE_SUCCESS;
256f3c22059SEtienne Carriere }
257f3c22059SEtienne Carriere 
258*ea8ba295SGatien Chevallier static TEE_Result stm32_rng_probe(const void *fdt, int offs,
259*ea8ba295SGatien Chevallier 				  const void *compat_data __unused)
260*ea8ba295SGatien Chevallier {
261*ea8ba295SGatien Chevallier 	TEE_Result res = TEE_ERROR_GENERIC;
262*ea8ba295SGatien Chevallier 
263*ea8ba295SGatien Chevallier 	/* Expect a single RNG instance */
264*ea8ba295SGatien Chevallier 	assert(!stm32_rng);
265*ea8ba295SGatien Chevallier 
266*ea8ba295SGatien Chevallier 	stm32_rng = calloc(1, sizeof(*stm32_rng));
267*ea8ba295SGatien Chevallier 	if (!stm32_rng)
268*ea8ba295SGatien Chevallier 		panic();
269*ea8ba295SGatien Chevallier 
270*ea8ba295SGatien Chevallier 	res = stm32_rng_parse_fdt(fdt, offs);
271*ea8ba295SGatien Chevallier 	if (res)
272*ea8ba295SGatien Chevallier 		goto err;
273*ea8ba295SGatien Chevallier 
274*ea8ba295SGatien Chevallier 	res = clk_enable(stm32_rng->clock);
275*ea8ba295SGatien Chevallier 	if (res)
276*ea8ba295SGatien Chevallier 		goto err;
277*ea8ba295SGatien Chevallier 
278*ea8ba295SGatien Chevallier 	if (stm32_rng->rstctrl &&
279*ea8ba295SGatien Chevallier 	    rstctrl_assert_to(stm32_rng->rstctrl, RNG_RESET_TIMEOUT_US)) {
280*ea8ba295SGatien Chevallier 		res = TEE_ERROR_GENERIC;
281*ea8ba295SGatien Chevallier 		goto err_clk;
282*ea8ba295SGatien Chevallier 	}
283*ea8ba295SGatien Chevallier 
284*ea8ba295SGatien Chevallier 	if (stm32_rng->rstctrl &&
285*ea8ba295SGatien Chevallier 	    rstctrl_deassert_to(stm32_rng->rstctrl, RNG_RESET_TIMEOUT_US)) {
286*ea8ba295SGatien Chevallier 		res = TEE_ERROR_GENERIC;
287*ea8ba295SGatien Chevallier 		goto err_clk;
288*ea8ba295SGatien Chevallier 	}
289*ea8ba295SGatien Chevallier 
290*ea8ba295SGatien Chevallier 	clk_disable(stm32_rng->clock);
291*ea8ba295SGatien Chevallier 
292*ea8ba295SGatien Chevallier 	if (stm32_rng->release_post_boot)
293*ea8ba295SGatien Chevallier 		stm32mp_register_non_secure_periph_iomem(stm32_rng->base.pa);
294*ea8ba295SGatien Chevallier 	else
295*ea8ba295SGatien Chevallier 		stm32mp_register_secure_periph_iomem(stm32_rng->base.pa);
296*ea8ba295SGatien Chevallier 
297*ea8ba295SGatien Chevallier 	return TEE_SUCCESS;
298*ea8ba295SGatien Chevallier 
299*ea8ba295SGatien Chevallier err_clk:
300*ea8ba295SGatien Chevallier 	clk_disable(stm32_rng->clock);
301*ea8ba295SGatien Chevallier err:
302*ea8ba295SGatien Chevallier 	free(stm32_rng);
303*ea8ba295SGatien Chevallier 	stm32_rng = NULL;
304*ea8ba295SGatien Chevallier 
305*ea8ba295SGatien Chevallier 	return res;
306*ea8ba295SGatien Chevallier }
307*ea8ba295SGatien Chevallier 
308*ea8ba295SGatien Chevallier static const struct dt_device_match rng_match_table[] = {
309*ea8ba295SGatien Chevallier 	{ .compatible = "st,stm32-rng" },
310*ea8ba295SGatien Chevallier 	{ .compatible = "st,stm32mp13-rng" },
311*ea8ba295SGatien Chevallier 	{ }
312*ea8ba295SGatien Chevallier };
313*ea8ba295SGatien Chevallier 
314*ea8ba295SGatien Chevallier DEFINE_DT_DRIVER(stm32_rng_dt_driver) = {
315*ea8ba295SGatien Chevallier 	.name = "stm32_rng",
316*ea8ba295SGatien Chevallier 	.match_table = rng_match_table,
317*ea8ba295SGatien Chevallier 	.probe = stm32_rng_probe,
318*ea8ba295SGatien Chevallier };
319d8682c4cSEtienne Carriere 
320d8682c4cSEtienne Carriere static TEE_Result stm32_rng_release(void)
321d8682c4cSEtienne Carriere {
322d8682c4cSEtienne Carriere 	if (stm32_rng && stm32_rng->release_post_boot) {
323d8682c4cSEtienne Carriere 		DMSG("Release RNG driver");
324d8682c4cSEtienne Carriere 		assert(!stm32_rng->refcount);
325d8682c4cSEtienne Carriere 		free(stm32_rng);
326d8682c4cSEtienne Carriere 		stm32_rng = NULL;
327d8682c4cSEtienne Carriere 	}
328d8682c4cSEtienne Carriere 
329d8682c4cSEtienne Carriere 	return TEE_SUCCESS;
330d8682c4cSEtienne Carriere }
331d8682c4cSEtienne Carriere 
332d8682c4cSEtienne Carriere release_init_resource(stm32_rng_release);
333f3c22059SEtienne Carriere #endif /*CFG_EMBED_DTB*/
334