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