xref: /rk3399_rockchip-uboot/include/rockchip/rkce_core.h (revision 1427f7c613d4175445bb319207dfc81d4a9ab254)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 /* Copyright (c) 2025 Rockchip Electronics Co., Ltd. */
4 
5 #ifndef __RKCE_CORE_H__
6 #define __RKCE_CORE_H__
7 
8 #include <linux/bitops.h>
9 #include <linux/types.h>
10 
11 #include "rkce_buf.h"
12 #include "rkce_error.h"
13 #include "rkce_reg.h"
14 
15 #define RKCE_TD_SG_NUM			8
16 
17 #define RKCE_AES_BLOCK_SIZE		16
18 #define RKCE_AES_KEYSIZE_128		16
19 #define RKCE_AES_KEYSIZE_192		24
20 #define RKCE_AES_KEYSIZE_256		32
21 
22 #define RKCE_SM4_KEYSIZE		16
23 
24 #define RKCE_DES_BLOCK_SIZE		8
25 #define RKCE_DES_KEYSIZE		8
26 #define RKCE_TDES_EDE_KEYSIZE		24
27 
28 #define RKCE_TD_ALIGINMENT		16
29 #define RKCE_TD_KEY_SIZE		128
30 #define RKCE_TD_IV_SIZE			16
31 #define RKCE_TD_GCM_LEN_SIZE		16
32 #define RKCE_TD_HASH_CTX_SIZE		RKCE_HASH_CONTEXT_SIZE
33 #define RKCE_TD_SYMM_CTX_SIZE		RKCE_SYMM_CONTEXT_SIZE
34 #define RKCE_TD_TAG_SIZE		16
35 #define RKCE_TD_TAG_SIZE_MIN		8
36 #define RKCE_TD_TAG_SIZE_MAX		RKCE_TD_TAG_SIZE
37 #define RKCE_TD_HASH_SIZE		64
38 #define RKCE_TD_FIFO_DEPTH		8
39 
40 #define RKCE_RESET_SYMM			BIT(0)
41 #define RKCE_RESET_HASH			BIT(1)
42 #define RKCE_RESET_PKA			BIT(2)
43 #define RKCE_RESET_ALL			(RKCE_RESET_SYMM | RKCE_RESET_HASH | RKCE_RESET_PKA)
44 
45 #define RKCE_WRITE_MASK_SHIFT		(16)
46 #define RKCE_WRITE_MASK_ALL		((0xffffu << RKCE_WRITE_MASK_SHIFT))
47 
48 enum rkce_expand_bit {
49 	RKCE_EXPAND_BIT_4G = 0,
50 	RKCE_EXPAND_BIT_8G,
51 	RKCE_EXPAND_BIT_16G,
52 	RKCE_EXPAND_BIT_32G,
53 };
54 
55 enum rkce_td_type {
56 	RKCE_TD_TYPE_SYMM = 0,
57 	RKCE_TD_TYPE_HASH,
58 	RKCE_TD_TYPE_SYMM_HASH_IN,
59 	RKCE_TD_TYPE_SYMM_HASH_OUT,
60 	RKCE_TD_TYPE_MAX,
61 };
62 
63 enum rkce_algo_symm_type {
64 	RKCE_SYMM_ALGO_AES = 0,
65 	RKCE_SYMM_ALGO_SM4,
66 	RKCE_SYMM_ALGO_DES,
67 	RKCE_SYMM_ALGO_TDES,
68 	RKCE_SYMM_ALGO_MAX,
69 };
70 
71 enum rkce_algo_symm_mode {
72 	RKCE_SYMM_MODE_ECB = 0,
73 	RKCE_SYMM_MODE_CBC,
74 	RKCE_SYMM_MODE_CTS,
75 	RKCE_SYMM_MODE_CTR,
76 	RKCE_SYMM_MODE_CFB,
77 	RKCE_SYMM_MODE_OFB,
78 	RKCE_SYMM_MODE_XTS,
79 	RKCE_SYMM_MODE_CCM,
80 	RKCE_SYMM_MODE_GCM,
81 	RKCE_SYMM_MODE_CMAC,
82 	RKCE_SYMM_MODE_CBC_MAC,
83 	RKCE_SYMM_MODE_BYPASS = 0xf,
84 	RKCE_SYMM_MODE_MAX,
85 };
86 
87 enum {
88 	RKCE_KEY_AES_128 = 0,
89 	RKCE_KEY_AES_192,
90 	RKCE_KEY_AES_256,
91 };
92 
93 enum rkce_algo_hash_type {
94 	RKCE_HASH_ALGO_SHA1 = 0,
95 	RKCE_HASH_ALGO_MD5,
96 	RKCE_HASH_ALGO_SHA256,
97 	RKCE_HASH_ALGO_SHA224,
98 	RKCE_HASH_ALGO_SM3 = 6,
99 	RKCE_HASH_ALGO_SHA512 = 8,
100 	RKCE_HASH_ALGO_SHA384 = 9,
101 	RKCE_HASH_ALGO_SHA512_224,
102 	RKCE_HASH_ALGO_SHA512_256,
103 	RKCE_HASH_ALGO_MAX,
104 };
105 
106 enum rkce_algo_asym_type {
107 	RKCE_ASYM_ALGO_RSA = 0,
108 	RKCE_ASYM_ALGO_ECC_P192,
109 	RKCE_ASYM_ALGO_ECC_P224,
110 	RKCE_ASYM_ALGO_ECC_P256,
111 	RKCE_ASYM_ALGO_ECC_P384,
112 	RKCE_ASYM_ALGO_ECC_P521,
113 	RKCE_ASYM_ALGO_SM2,
114 	RKCE_ASYM_ALGO_MAX,
115 };
116 
117 enum rkce_algo_type {
118 	RKCE_ALGO_TYPE_HASH,
119 	RKCE_ALGO_TYPE_HMAC,
120 	RKCE_ALGO_TYPE_CIPHER,
121 	RKCE_ALGO_TYPE_ASYM,
122 	RKCE_ALGO_TYPE_AEAD,
123 	RKCE_ALGO_TYPE_MAX,
124 };
125 
126 struct rkce_ip_info {
127 	uint32_t	aes_ver;
128 	uint32_t	des_ver;
129 	uint32_t	sm4_ver;
130 	uint32_t	hash_ver;
131 	uint32_t	hmac_ver;
132 	uint32_t	pka_ver;
133 	uint32_t	extra_feature;
134 	uint32_t	ce_ver;
135 };
136 
137 struct rkce_gcm_len {
138 	uint32_t	pc_len_l;
139 	uint32_t	pc_len_h;
140 	uint32_t	aad_len_l;
141 	uint32_t	aad_len_h;
142 };
143 
144 struct rkce_sg_info {
145 	uint32_t	src_size;
146 	uint32_t	src_addr_h;
147 	uint32_t	src_addr_l;
148 
149 	uint32_t	dst_size;
150 	uint32_t	dst_addr_h;
151 	uint32_t	dst_addr_l;
152 };
153 
154 /* total = 64 + 16 + 16 + 16 + 32 = 114(Byte) */
155 struct rkce_symm_td_buf {
156 	uint8_t			key1[RKCE_AES_KEYSIZE_256];		// offset 0x00
157 	uint8_t			key2[RKCE_AES_KEYSIZE_256];		// offset 0x20
158 	uint8_t			iv[RKCE_TD_IV_SIZE];			// offset 0x40
159 	struct rkce_gcm_len	gcm_len;				// offset 0x50
160 	uint8_t			tag[RKCE_TD_TAG_SIZE];			// offset 0x60
161 	uint8_t			ctx[RKCE_SYMM_CONTEXT_SIZE];		// offset 0x70
162 	void			*user_data;
163 };
164 
165 /* total = 128 + 64 + 208 = 360(Byte) */
166 struct rkce_hash_td_buf {
167 	uint8_t			key[RKCE_TD_KEY_SIZE];			// offset 0x00
168 	uint8_t			hash[RKCE_TD_HASH_SIZE];		// offset 0x80
169 	uint8_t			ctx[RKCE_HASH_CONTEXT_SIZE];		// offset 0xB0
170 	void			*user_data;
171 };
172 
173 struct rkce_symm_hash_td_buf {
174 	uint8_t			key1[RKCE_AES_KEYSIZE_256];		// offset 0x00
175 	uint8_t			key2[RKCE_AES_KEYSIZE_256];		// offset 0x20
176 	uint8_t			key3[RKCE_AES_KEYSIZE_256 * 2];		// offset 0x40
177 	uint8_t			iv[RKCE_TD_IV_SIZE];			// offset 0x80
178 	struct rkce_gcm_len	gcm_len;				// offset 0x90
179 	uint8_t			tag[RKCE_TD_TAG_SIZE];			// offset 0xA0
180 	uint8_t			hash[RKCE_TD_HASH_SIZE];		// offset 0xB0
181 	uint8_t			symm_ctx[RKCE_SYMM_CONTEXT_SIZE];	// offset 0xF0
182 	uint8_t			hash_ctx[RKCE_HASH_CONTEXT_SIZE];	// offset 0x110
183 	void			*user_data;
184 };
185 
186 struct rkce_symm_td_ctrl {
187 	uint32_t	td_type : 2;
188 	uint32_t	is_dec : 1;
189 	uint32_t	is_aad : 1;
190 	uint32_t	symm_algo : 2;
191 	uint32_t : 2;
192 	uint32_t	symm_mode : 4;
193 	uint32_t	key_size : 2;
194 	uint32_t	first_pkg : 1;
195 	uint32_t	last_pkg : 1;
196 	uint32_t	key_sel : 3;
197 	uint32_t	iv_len : 5;
198 	uint32_t : 4;
199 	uint32_t	is_key_inside : 1;
200 	uint32_t : 1;
201 	uint32_t	is_preemptible : 1;
202 	uint32_t	int_en : 1;
203 };
204 
205 struct rkce_hash_td_ctrl {
206 	uint32_t	td_type : 2;
207 	uint32_t : 5;
208 	uint32_t	hw_pad_en : 1;
209 	uint32_t : 6;
210 	uint32_t	first_pkg : 1;
211 	uint32_t	last_pkg : 1;
212 	uint32_t : 8;
213 	uint32_t	hash_algo: 4;
214 	uint32_t : 1;
215 	uint32_t	hmac_en : 1;
216 	uint32_t	is_preemptible : 1;
217 	uint32_t	int_en : 1;
218 };
219 
220 struct rkce_symm_hash_td_ctrl {
221 	uint32_t	td_type : 2;
222 	uint32_t	is_dec : 1;
223 	uint32_t	is_aad : 1;
224 	uint32_t	symm_algo : 2;
225 	uint32_t : 1;
226 	uint32_t	hw_pad_en : 1;
227 	uint32_t	symm_mode : 4;
228 	uint32_t	key_size : 2;
229 	uint32_t	first_pkg : 1;
230 	uint32_t	last_pkg : 1;
231 	uint32_t	key_sel : 3;
232 	uint32_t	iv_len : 5;
233 	uint32_t	hash_algo: 4;
234 	uint32_t	is_key_inside : 1;
235 	uint32_t	hmac_en : 1;
236 	uint32_t	is_preemptible : 1;
237 	uint32_t	int_en : 1;
238 };
239 
240 struct rkce_symm_td {
241 	uint32_t			task_id;
242 	struct rkce_symm_td_ctrl	ctrl;
243 	uint32_t			reserve1;
244 	uint32_t			key_addr;
245 
246 	uint32_t			iv_addr;
247 	uint32_t			gcm_len_addr;
248 	uint32_t			reserve2;
249 	uint32_t			tag_addr;
250 
251 	struct rkce_sg_info		sg[RKCE_TD_SG_NUM];
252 
253 	uint32_t			reserve3;
254 	uint32_t			symm_ctx_addr;
255 	uint32_t			reserve4[5];
256 	uint32_t			next_task;
257 };
258 
259 struct rkce_hash_td {
260 	uint32_t			task_id;
261 	struct rkce_hash_td_ctrl	ctrl;
262 	uint32_t			reserve1;
263 	uint32_t			key_addr;
264 
265 	uint32_t			reserve2[2];
266 	uint32_t			hash_addr;
267 	uint32_t			reserve3;
268 
269 	struct rkce_sg_info		sg[RKCE_TD_SG_NUM];
270 
271 	uint32_t			hash_ctx_addr;
272 	uint32_t			reserve4[6];
273 	uint32_t			next_task;
274 };
275 
276 struct rkce_symm_hash_td {
277 	uint32_t			task_id;
278 	struct rkce_symm_hash_td_ctrl	ctrl;
279 	uint32_t			reserve1;
280 	uint32_t			key_addr;
281 
282 	uint32_t			iv_addr;
283 	uint32_t			gcm_len_addr;
284 	uint32_t			hash_addr;
285 	uint32_t			tag_addr;
286 
287 	struct rkce_sg_info		sg[RKCE_TD_SG_NUM];
288 
289 	uint32_t			hash_ctx_addr;
290 	uint32_t			symm_ctx_addr;
291 	uint32_t			reserve3[5];
292 	uint32_t			next_task;
293 };
294 
295 struct rkce_td {
296 	union {
297 		struct rkce_symm_td	 symm;
298 		struct rkce_hash_td	 hash;
299 		struct rkce_symm_hash_td symm_hash;
300 	} td;
301 };
302 
303 struct rkce_td_buf {
304 	union {
305 		struct rkce_symm_td_buf	     symm;
306 		struct rkce_hash_td_buf	     hash;
307 		struct rkce_symm_hash_td_buf symm_hash;
308 	} td_buf;
309 };
310 
311 typedef int (*request_cb_func)(int result, uint32_t td_id, void *td_virt);
312 
313 void rkce_dump_reginfo(void *rkce_hw);
314 
315 void *rkce_hardware_alloc(void __iomem *reg_base);
316 
317 void rkce_hardware_free(void *rkce_hw);
318 
319 void rkce_irq_handler(void *rkce_hw);
320 
321 void rkce_irq_thread(void *rkce_hw);
322 
323 int rkce_irq_callback_set(void *rkce_hw, enum rkce_td_type td_type, request_cb_func cb_func);
324 
325 int rkce_soft_reset(void *rkce_hw, uint32_t reset_sel);
326 
327 int rkce_push_td(void *rkce_hw, void *td);
328 
329 int rkce_push_td_sync(void *rkce_hw, void *td, uint32_t timeout_ms);
330 
331 uint32_t rkce_get_td_type(void *td_buf);
332 
333 int rkce_init_symm_td(struct rkce_symm_td *td, struct rkce_symm_td_buf *buf);
334 
335 int rkce_init_hash_td(struct rkce_hash_td *td, struct rkce_hash_td_buf *buf);
336 
337 bool rkce_hw_algo_valid(void *rkce_hw, uint32_t type, uint32_t algo, uint32_t mode);
338 
339 #endif
340