xref: /optee_os/core/drivers/stm32_rng.c (revision 23123473c8e341cea1fbc833c1828194b3b9d983)
1f3c22059SEtienne Carriere // SPDX-License-Identifier: BSD-3-Clause
2f3c22059SEtienne Carriere /*
3f3c22059SEtienne Carriere  * Copyright (c) 2018-2019, STMicroelectronics
4f3c22059SEtienne Carriere  */
5f3c22059SEtienne Carriere 
6f3c22059SEtienne Carriere #include <assert.h>
7d7a1a7d2SEtienne Carriere #include <drivers/clk.h>
8d7a1a7d2SEtienne Carriere #include <drivers/clk_dt.h>
9f3c22059SEtienne Carriere #include <drivers/stm32_rng.h>
10f3c22059SEtienne Carriere #include <io.h>
11f3c22059SEtienne Carriere #include <kernel/delay.h>
12a2fc83d1SJerome Forissier #include <kernel/dt.h>
1365401337SJens Wiklander #include <kernel/boot.h>
14f3c22059SEtienne Carriere #include <kernel/panic.h>
1565b5ada4SMarouene Boubakri #include <kernel/thread.h>
16a2fc83d1SJerome Forissier #include <libfdt.h>
17f3c22059SEtienne Carriere #include <mm/core_memprot.h>
18f3c22059SEtienne Carriere #include <stdbool.h>
19f3c22059SEtienne Carriere #include <stm32_util.h>
20f3c22059SEtienne Carriere #include <string.h>
21f3c22059SEtienne Carriere 
22f3c22059SEtienne Carriere #define DT_RNG_COMPAT		"st,stm32-rng"
23f3c22059SEtienne Carriere #define RNG_CR			0x00U
24f3c22059SEtienne Carriere #define RNG_SR			0x04U
25f3c22059SEtienne Carriere #define RNG_DR			0x08U
26f3c22059SEtienne Carriere 
27f3c22059SEtienne Carriere #define RNG_CR_RNGEN		BIT(2)
28f3c22059SEtienne Carriere #define RNG_CR_IE		BIT(3)
29f3c22059SEtienne Carriere #define RNG_CR_CED		BIT(5)
30f3c22059SEtienne Carriere 
31f3c22059SEtienne Carriere #define RNG_SR_DRDY		BIT(0)
32f3c22059SEtienne Carriere #define RNG_SR_CECS		BIT(1)
33f3c22059SEtienne Carriere #define RNG_SR_SECS		BIT(2)
34f3c22059SEtienne Carriere #define RNG_SR_CEIS		BIT(5)
35f3c22059SEtienne Carriere #define RNG_SR_SEIS		BIT(6)
36f3c22059SEtienne Carriere 
37c99311c8SEtienne Carriere #define RNG_TIMEOUT_US		U(100000)
38f3c22059SEtienne Carriere 
39f3c22059SEtienne Carriere struct stm32_rng_instance {
40f3c22059SEtienne Carriere 	struct io_pa_va base;
41d7a1a7d2SEtienne Carriere 	struct clk *clock;
42f3c22059SEtienne Carriere 	unsigned int lock;
43f3c22059SEtienne Carriere 	unsigned int refcount;
44d8682c4cSEtienne Carriere 	bool release_post_boot;
45f3c22059SEtienne Carriere };
46f3c22059SEtienne Carriere 
47f3c22059SEtienne Carriere static struct stm32_rng_instance *stm32_rng;
48f3c22059SEtienne Carriere 
49f3c22059SEtienne Carriere /*
50f3c22059SEtienne Carriere  * Extracts from the STM32 RNG specification:
51f3c22059SEtienne Carriere  *
52f3c22059SEtienne Carriere  * When a noise source (or seed) error occurs, the RNG stops generating
53f3c22059SEtienne Carriere  * random numbers and sets to “1” both SEIS and SECS bits to indicate
54f3c22059SEtienne Carriere  * that a seed error occurred. (...)
55f3c22059SEtienne Carriere 
56f3c22059SEtienne Carriere  * The following sequence shall be used to fully recover from a seed
57f3c22059SEtienne Carriere  * error after the RNG initialization:
58f3c22059SEtienne Carriere  * 1. Clear the SEIS bit by writing it to “0”.
59f3c22059SEtienne Carriere  * 2. Read out 12 words from the RNG_DR register, and discard each of
60f3c22059SEtienne Carriere  * them in order to clean the pipeline.
61f3c22059SEtienne Carriere  * 3. Confirm that SEIS is still cleared. Random number generation is
62f3c22059SEtienne Carriere  * back to normal.
63f3c22059SEtienne Carriere  */
64f3c22059SEtienne Carriere static void conceal_seed_error(vaddr_t rng_base)
65f3c22059SEtienne Carriere {
66f3c22059SEtienne Carriere 	if (io_read32(rng_base + RNG_SR) & (RNG_SR_SECS | RNG_SR_SEIS)) {
67f3c22059SEtienne Carriere 		size_t i = 0;
68f3c22059SEtienne Carriere 
69f3c22059SEtienne Carriere 		io_mask32(rng_base + RNG_SR, 0, RNG_SR_SEIS);
70f3c22059SEtienne Carriere 
71f3c22059SEtienne Carriere 		for (i = 12; i != 0; i--)
72f3c22059SEtienne Carriere 			(void)io_read32(rng_base + RNG_DR);
73f3c22059SEtienne Carriere 
74f3c22059SEtienne Carriere 		if (io_read32(rng_base + RNG_SR) & RNG_SR_SEIS)
75f3c22059SEtienne Carriere 			panic("RNG noise");
76f3c22059SEtienne Carriere 	}
77f3c22059SEtienne Carriere }
78f3c22059SEtienne Carriere 
79f3c22059SEtienne Carriere #define RNG_FIFO_BYTE_DEPTH		16u
80f3c22059SEtienne Carriere 
81c99311c8SEtienne Carriere static TEE_Result read_available(vaddr_t rng_base, uint8_t *out, size_t *size)
82f3c22059SEtienne Carriere {
83c99311c8SEtienne Carriere 	uint8_t *buf = NULL;
84c99311c8SEtienne Carriere 	size_t req_size = 0;
85c99311c8SEtienne Carriere 	size_t len = 0;
86f3c22059SEtienne Carriere 
87f3c22059SEtienne Carriere 	conceal_seed_error(rng_base);
88f3c22059SEtienne Carriere 
89*23123473SEtienne Carriere 	if (!(io_read32(rng_base + RNG_SR) & RNG_SR_DRDY)) {
90*23123473SEtienne Carriere 		FMSG("RNG not ready");
91c99311c8SEtienne Carriere 		return TEE_ERROR_NO_DATA;
92*23123473SEtienne Carriere 	}
93f3c22059SEtienne Carriere 
94*23123473SEtienne Carriere 	if (io_read32(rng_base + RNG_SR) & RNG_SR_SEIS) {
95*23123473SEtienne Carriere 		FMSG("RNG noise error");
96c99311c8SEtienne Carriere 		return TEE_ERROR_NO_DATA;
97*23123473SEtienne Carriere 	}
98c99311c8SEtienne Carriere 
99c99311c8SEtienne Carriere 	buf = out;
100c99311c8SEtienne Carriere 	req_size = MIN(RNG_FIFO_BYTE_DEPTH, *size);
101c99311c8SEtienne Carriere 	len = req_size;
102f3c22059SEtienne Carriere 
103f3c22059SEtienne Carriere 	/* RNG is ready: read up to 4 32bit words */
104f3c22059SEtienne Carriere 	while (len) {
105f3c22059SEtienne Carriere 		uint32_t data32 = io_read32(rng_base + RNG_DR);
106f3c22059SEtienne Carriere 		size_t sz = MIN(len, sizeof(uint32_t));
107f3c22059SEtienne Carriere 
108f3c22059SEtienne Carriere 		memcpy(buf, &data32, sz);
109f3c22059SEtienne Carriere 		buf += sz;
110f3c22059SEtienne Carriere 		len -= sz;
111f3c22059SEtienne Carriere 	}
112c99311c8SEtienne Carriere 
113f3c22059SEtienne Carriere 	*size = req_size;
114f3c22059SEtienne Carriere 
115c99311c8SEtienne Carriere 	return TEE_SUCCESS;
116f3c22059SEtienne Carriere }
117f3c22059SEtienne Carriere 
118f3c22059SEtienne Carriere static void gate_rng(bool enable, struct stm32_rng_instance *dev)
119f3c22059SEtienne Carriere {
120c2e4eb43SAnton Rybakov 	vaddr_t rng_cr = io_pa_or_va(&dev->base, 1) + RNG_CR;
121f3c22059SEtienne Carriere 	uint32_t exceptions = may_spin_lock(&dev->lock);
122f3c22059SEtienne Carriere 
123f3c22059SEtienne Carriere 	if (enable) {
124f3c22059SEtienne Carriere 		/* incr_refcnt return non zero if resource shall be enabled */
125f3c22059SEtienne Carriere 		if (incr_refcnt(&dev->refcount)) {
126*23123473SEtienne Carriere 			FMSG("enable RNG");
127d7a1a7d2SEtienne Carriere 			clk_enable(dev->clock);
128f3c22059SEtienne Carriere 			io_write32(rng_cr, 0);
129f3c22059SEtienne Carriere 			io_write32(rng_cr, RNG_CR_RNGEN | RNG_CR_CED);
130f3c22059SEtienne Carriere 		}
131f3c22059SEtienne Carriere 	} else {
132f3c22059SEtienne Carriere 		/* decr_refcnt return non zero if resource shall be disabled */
133f3c22059SEtienne Carriere 		if (decr_refcnt(&dev->refcount)) {
134*23123473SEtienne Carriere 			FMSG("disable RNG");
135f3c22059SEtienne Carriere 			io_write32(rng_cr, 0);
136d7a1a7d2SEtienne Carriere 			clk_disable(dev->clock);
137f3c22059SEtienne Carriere 		}
138f3c22059SEtienne Carriere 	}
139f3c22059SEtienne Carriere 
140f3c22059SEtienne Carriere 	may_spin_unlock(&dev->lock, exceptions);
141f3c22059SEtienne Carriere }
142f3c22059SEtienne Carriere 
143f3c22059SEtienne Carriere TEE_Result stm32_rng_read(uint8_t *out, size_t size)
144f3c22059SEtienne Carriere {
145c99311c8SEtienne Carriere 	TEE_Result rc = TEE_ERROR_GENERIC;
146c99311c8SEtienne Carriere 	bool burst_timeout = false;
147c99311c8SEtienne Carriere 	uint64_t timeout_ref = 0;
148f3c22059SEtienne Carriere 	uint32_t exceptions = 0;
149f3c22059SEtienne Carriere 	uint8_t *out_ptr = out;
150c99311c8SEtienne Carriere 	vaddr_t rng_base = 0;
151f3c22059SEtienne Carriere 	size_t out_size = 0;
152f3c22059SEtienne Carriere 
153f3c22059SEtienne Carriere 	if (!stm32_rng) {
154f3c22059SEtienne Carriere 		DMSG("No RNG");
155f3c22059SEtienne Carriere 		return TEE_ERROR_NOT_SUPPORTED;
156f3c22059SEtienne Carriere 	}
157f3c22059SEtienne Carriere 
158f3c22059SEtienne Carriere 	gate_rng(true, stm32_rng);
159c99311c8SEtienne Carriere 	rng_base = io_pa_or_va(&stm32_rng->base, 1);
160c99311c8SEtienne Carriere 
161c99311c8SEtienne Carriere 	/* Arm timeout */
162c99311c8SEtienne Carriere 	timeout_ref = timeout_init_us(RNG_TIMEOUT_US);
163c99311c8SEtienne Carriere 	burst_timeout = false;
164f3c22059SEtienne Carriere 
165f3c22059SEtienne Carriere 	while (out_size < size) {
166f3c22059SEtienne Carriere 		/* Read by chunks of the size the RNG FIFO depth */
167f3c22059SEtienne Carriere 		size_t sz = size - out_size;
168f3c22059SEtienne Carriere 
169f3c22059SEtienne Carriere 		exceptions = may_spin_lock(&stm32_rng->lock);
170f3c22059SEtienne Carriere 
171c99311c8SEtienne Carriere 		rc = read_available(rng_base, out_ptr, &sz);
172c99311c8SEtienne Carriere 
173c99311c8SEtienne Carriere 		/* Raise timeout only if we failed to get some samples */
174c99311c8SEtienne Carriere 		assert(!rc || rc == TEE_ERROR_NO_DATA);
175c99311c8SEtienne Carriere 		if (rc)
176c99311c8SEtienne Carriere 			burst_timeout = timeout_elapsed(timeout_ref);
177f3c22059SEtienne Carriere 
178f3c22059SEtienne Carriere 		may_spin_unlock(&stm32_rng->lock, exceptions);
179f3c22059SEtienne Carriere 
180c99311c8SEtienne Carriere 		if (burst_timeout) {
181c99311c8SEtienne Carriere 			rc = TEE_ERROR_GENERIC;
182c99311c8SEtienne Carriere 			goto out;
183f3c22059SEtienne Carriere 		}
184f3c22059SEtienne Carriere 
185c99311c8SEtienne Carriere 		if (!rc) {
186c99311c8SEtienne Carriere 			out_size += sz;
187c99311c8SEtienne Carriere 			out_ptr += sz;
188c99311c8SEtienne Carriere 			/* Re-arm timeout */
189c99311c8SEtienne Carriere 			timeout_ref = timeout_init_us(RNG_TIMEOUT_US);
190c99311c8SEtienne Carriere 			burst_timeout = false;
191c99311c8SEtienne Carriere 		}
192c99311c8SEtienne Carriere 	}
193c99311c8SEtienne Carriere 
194c99311c8SEtienne Carriere out:
195c99311c8SEtienne Carriere 	assert(!rc || rc == TEE_ERROR_GENERIC);
196f3c22059SEtienne Carriere 	gate_rng(false, stm32_rng);
197f3c22059SEtienne Carriere 
198f3c22059SEtienne Carriere 	return rc;
199f3c22059SEtienne Carriere }
200f3c22059SEtienne Carriere 
201f3c22059SEtienne Carriere #ifdef CFG_EMBED_DTB
202f3c22059SEtienne Carriere static TEE_Result stm32_rng_init(void)
203f3c22059SEtienne Carriere {
204f3c22059SEtienne Carriere 	void *fdt = NULL;
205f3c22059SEtienne Carriere 	int node = -1;
206f3c22059SEtienne Carriere 	struct dt_node_info dt_info;
207d7a1a7d2SEtienne Carriere 	TEE_Result res = TEE_ERROR_GENERIC;
208f3c22059SEtienne Carriere 
209f3c22059SEtienne Carriere 	memset(&dt_info, 0, sizeof(dt_info));
210f3c22059SEtienne Carriere 
211f3c22059SEtienne Carriere 	fdt = get_embedded_dt();
212f3c22059SEtienne Carriere 	if (!fdt)
213f3c22059SEtienne Carriere 		panic();
214f3c22059SEtienne Carriere 
215f3c22059SEtienne Carriere 	while (true) {
216f3c22059SEtienne Carriere 		node = fdt_node_offset_by_compatible(fdt, node, DT_RNG_COMPAT);
217f3c22059SEtienne Carriere 		if (node < 0)
218f3c22059SEtienne Carriere 			break;
219f3c22059SEtienne Carriere 
220f3c22059SEtienne Carriere 		_fdt_fill_device_info(fdt, &dt_info, node);
221f3c22059SEtienne Carriere 
222f3c22059SEtienne Carriere 		if (!(dt_info.status & DT_STATUS_OK_SEC))
223f3c22059SEtienne Carriere 			continue;
224f3c22059SEtienne Carriere 
225f3c22059SEtienne Carriere 		if (stm32_rng)
226f3c22059SEtienne Carriere 			panic();
227f3c22059SEtienne Carriere 
228f3c22059SEtienne Carriere 		stm32_rng = calloc(1, sizeof(*stm32_rng));
229f3c22059SEtienne Carriere 		if (!stm32_rng)
230f3c22059SEtienne Carriere 			panic();
231f3c22059SEtienne Carriere 
232f3c22059SEtienne Carriere 		assert(dt_info.clock != DT_INFO_INVALID_CLOCK &&
233aabd492eSLionel Debieve 		       dt_info.reg != DT_INFO_INVALID_REG &&
234aabd492eSLionel Debieve 		       dt_info.reg_size != DT_INFO_INVALID_REG_SIZE);
235f3c22059SEtienne Carriere 
23628f25d8dSEtienne Carriere 		if (dt_info.status & DT_STATUS_OK_NSEC) {
23728f25d8dSEtienne Carriere 			stm32mp_register_non_secure_periph_iomem(dt_info.reg);
238d8682c4cSEtienne Carriere 			stm32_rng->release_post_boot = true;
23928f25d8dSEtienne Carriere 		} else {
24028f25d8dSEtienne Carriere 			stm32mp_register_secure_periph_iomem(dt_info.reg);
24128f25d8dSEtienne Carriere 		}
24228f25d8dSEtienne Carriere 
243f3c22059SEtienne Carriere 		stm32_rng->base.pa = dt_info.reg;
244d8682c4cSEtienne Carriere 		if (!io_pa_or_va_secure(&stm32_rng->base, dt_info.reg_size))
245d8682c4cSEtienne Carriere 			panic();
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 
251d7a1a7d2SEtienne Carriere 		assert(stm32_rng->clock);
252f3c22059SEtienne Carriere 
253f3c22059SEtienne Carriere 		DMSG("RNG init");
254f3c22059SEtienne Carriere 	}
255f3c22059SEtienne Carriere 
256f3c22059SEtienne Carriere 	return TEE_SUCCESS;
257f3c22059SEtienne Carriere }
258f3c22059SEtienne Carriere 
259d8682c4cSEtienne Carriere early_init_late(stm32_rng_init);
260d8682c4cSEtienne Carriere 
261d8682c4cSEtienne Carriere static TEE_Result stm32_rng_release(void)
262d8682c4cSEtienne Carriere {
263d8682c4cSEtienne Carriere 	if (stm32_rng && stm32_rng->release_post_boot) {
264d8682c4cSEtienne Carriere 		DMSG("Release RNG driver");
265d8682c4cSEtienne Carriere 		assert(!stm32_rng->refcount);
266d8682c4cSEtienne Carriere 		free(stm32_rng);
267d8682c4cSEtienne Carriere 		stm32_rng = NULL;
268d8682c4cSEtienne Carriere 	}
269d8682c4cSEtienne Carriere 
270d8682c4cSEtienne Carriere 	return TEE_SUCCESS;
271d8682c4cSEtienne Carriere }
272d8682c4cSEtienne Carriere 
273d8682c4cSEtienne Carriere release_init_resource(stm32_rng_release);
274f3c22059SEtienne Carriere #endif /*CFG_EMBED_DTB*/
275