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 { 94 RKCE_KEY_SEL_USER = 0, 95 RKCE_KEY_SEL_KT = 1, 96 RKCE_KEY_SEL_KL_KA = 4, 97 RKCE_KEY_SEL_KL_K1, 98 RKCE_KEY_SEL_KL_K2, 99 RKCE_KEY_SEL_KL_K3, 100 }; 101 102 enum rkce_algo_hash_type { 103 RKCE_HASH_ALGO_SHA1 = 0, 104 RKCE_HASH_ALGO_MD5, 105 RKCE_HASH_ALGO_SHA256, 106 RKCE_HASH_ALGO_SHA224, 107 RKCE_HASH_ALGO_SM3 = 6, 108 RKCE_HASH_ALGO_SHA512 = 8, 109 RKCE_HASH_ALGO_SHA384 = 9, 110 RKCE_HASH_ALGO_SHA512_224, 111 RKCE_HASH_ALGO_SHA512_256, 112 RKCE_HASH_ALGO_MAX, 113 }; 114 115 enum rkce_algo_asym_type { 116 RKCE_ASYM_ALGO_RSA = 0, 117 RKCE_ASYM_ALGO_ECC_P192, 118 RKCE_ASYM_ALGO_ECC_P224, 119 RKCE_ASYM_ALGO_ECC_P256, 120 RKCE_ASYM_ALGO_ECC_P384, 121 RKCE_ASYM_ALGO_ECC_P521, 122 RKCE_ASYM_ALGO_SM2, 123 RKCE_ASYM_ALGO_MAX, 124 }; 125 126 enum rkce_algo_type { 127 RKCE_ALGO_TYPE_HASH, 128 RKCE_ALGO_TYPE_HMAC, 129 RKCE_ALGO_TYPE_CIPHER, 130 RKCE_ALGO_TYPE_ASYM, 131 RKCE_ALGO_TYPE_AEAD, 132 RKCE_ALGO_TYPE_MAX, 133 }; 134 135 struct rkce_ip_info { 136 uint32_t aes_ver; 137 uint32_t des_ver; 138 uint32_t sm4_ver; 139 uint32_t hash_ver; 140 uint32_t hmac_ver; 141 uint32_t pka_ver; 142 uint32_t extra_feature; 143 uint32_t ce_ver; 144 }; 145 146 struct rkce_gcm_len { 147 uint32_t pc_len_l; 148 uint32_t pc_len_h; 149 uint32_t aad_len_l; 150 uint32_t aad_len_h; 151 }; 152 153 struct rkce_sg_info { 154 uint32_t src_size; 155 uint32_t src_addr_h; 156 uint32_t src_addr_l; 157 158 uint32_t dst_size; 159 uint32_t dst_addr_h; 160 uint32_t dst_addr_l; 161 }; 162 163 /* total = 64 + 16 + 16 + 16 + 32 = 114(Byte) */ 164 struct rkce_symm_td_buf { 165 uint8_t key1[RKCE_AES_KEYSIZE_256]; // offset 0x00 166 uint8_t key2[RKCE_AES_KEYSIZE_256]; // offset 0x20 167 uint8_t iv[RKCE_TD_IV_SIZE]; // offset 0x40 168 struct rkce_gcm_len gcm_len; // offset 0x50 169 uint8_t tag[RKCE_TD_TAG_SIZE]; // offset 0x60 170 uint8_t ctx[RKCE_SYMM_CONTEXT_SIZE]; // offset 0x70 171 void *user_data; 172 }; 173 174 /* total = 128 + 64 + 208 = 360(Byte) */ 175 struct rkce_hash_td_buf { 176 uint8_t key[RKCE_TD_KEY_SIZE]; // offset 0x00 177 uint8_t hash[RKCE_TD_HASH_SIZE]; // offset 0x80 178 uint8_t ctx[RKCE_HASH_CONTEXT_SIZE]; // offset 0xB0 179 void *user_data; 180 }; 181 182 struct rkce_symm_hash_td_buf { 183 uint8_t key1[RKCE_AES_KEYSIZE_256]; // offset 0x00 184 uint8_t key2[RKCE_AES_KEYSIZE_256]; // offset 0x20 185 uint8_t key3[RKCE_AES_KEYSIZE_256 * 2]; // offset 0x40 186 uint8_t iv[RKCE_TD_IV_SIZE]; // offset 0x80 187 struct rkce_gcm_len gcm_len; // offset 0x90 188 uint8_t tag[RKCE_TD_TAG_SIZE]; // offset 0xA0 189 uint8_t hash[RKCE_TD_HASH_SIZE]; // offset 0xB0 190 uint8_t symm_ctx[RKCE_SYMM_CONTEXT_SIZE]; // offset 0xF0 191 uint8_t hash_ctx[RKCE_HASH_CONTEXT_SIZE]; // offset 0x110 192 void *user_data; 193 }; 194 195 struct rkce_symm_td_ctrl { 196 uint32_t td_type : 2; 197 uint32_t is_dec : 1; 198 uint32_t is_aad : 1; 199 uint32_t symm_algo : 2; 200 uint32_t : 2; 201 uint32_t symm_mode : 4; 202 uint32_t key_size : 2; 203 uint32_t first_pkg : 1; 204 uint32_t last_pkg : 1; 205 uint32_t key_sel : 3; 206 uint32_t iv_len : 5; 207 uint32_t : 4; 208 uint32_t is_key_inside : 1; 209 uint32_t : 1; 210 uint32_t is_preemptible : 1; 211 uint32_t int_en : 1; 212 }; 213 214 struct rkce_hash_td_ctrl { 215 uint32_t td_type : 2; 216 uint32_t : 5; 217 uint32_t hw_pad_en : 1; 218 uint32_t : 6; 219 uint32_t first_pkg : 1; 220 uint32_t last_pkg : 1; 221 uint32_t : 8; 222 uint32_t hash_algo: 4; 223 uint32_t : 1; 224 uint32_t hmac_en : 1; 225 uint32_t is_preemptible : 1; 226 uint32_t int_en : 1; 227 }; 228 229 struct rkce_symm_hash_td_ctrl { 230 uint32_t td_type : 2; 231 uint32_t is_dec : 1; 232 uint32_t is_aad : 1; 233 uint32_t symm_algo : 2; 234 uint32_t : 1; 235 uint32_t hw_pad_en : 1; 236 uint32_t symm_mode : 4; 237 uint32_t key_size : 2; 238 uint32_t first_pkg : 1; 239 uint32_t last_pkg : 1; 240 uint32_t key_sel : 3; 241 uint32_t iv_len : 5; 242 uint32_t hash_algo: 4; 243 uint32_t is_key_inside : 1; 244 uint32_t hmac_en : 1; 245 uint32_t is_preemptible : 1; 246 uint32_t int_en : 1; 247 }; 248 249 struct rkce_symm_td { 250 uint32_t task_id; 251 struct rkce_symm_td_ctrl ctrl; 252 uint32_t reserve1; 253 uint32_t key_addr; 254 255 uint32_t iv_addr; 256 uint32_t gcm_len_addr; 257 uint32_t reserve2; 258 uint32_t tag_addr; 259 260 struct rkce_sg_info sg[RKCE_TD_SG_NUM]; 261 262 uint32_t reserve3; 263 uint32_t symm_ctx_addr; 264 uint32_t reserve4[5]; 265 uint32_t next_task; 266 }; 267 268 struct rkce_hash_td { 269 uint32_t task_id; 270 struct rkce_hash_td_ctrl ctrl; 271 uint32_t reserve1; 272 uint32_t key_addr; 273 274 uint32_t reserve2[2]; 275 uint32_t hash_addr; 276 uint32_t reserve3; 277 278 struct rkce_sg_info sg[RKCE_TD_SG_NUM]; 279 280 uint32_t hash_ctx_addr; 281 uint32_t reserve4[6]; 282 uint32_t next_task; 283 }; 284 285 struct rkce_symm_hash_td { 286 uint32_t task_id; 287 struct rkce_symm_hash_td_ctrl ctrl; 288 uint32_t reserve1; 289 uint32_t key_addr; 290 291 uint32_t iv_addr; 292 uint32_t gcm_len_addr; 293 uint32_t hash_addr; 294 uint32_t tag_addr; 295 296 struct rkce_sg_info sg[RKCE_TD_SG_NUM]; 297 298 uint32_t hash_ctx_addr; 299 uint32_t symm_ctx_addr; 300 uint32_t reserve3[5]; 301 uint32_t next_task; 302 }; 303 304 struct rkce_td { 305 union { 306 struct rkce_symm_td symm; 307 struct rkce_hash_td hash; 308 struct rkce_symm_hash_td symm_hash; 309 } td; 310 }; 311 312 struct rkce_td_buf { 313 union { 314 struct rkce_symm_td_buf symm; 315 struct rkce_hash_td_buf hash; 316 struct rkce_symm_hash_td_buf symm_hash; 317 } td_buf; 318 }; 319 320 typedef int (*request_cb_func)(int result, uint32_t td_id, void *td_virt); 321 322 void rkce_dump_reginfo(void *rkce_hw); 323 324 void *rkce_hardware_alloc(void __iomem *reg_base); 325 326 void rkce_hardware_free(void *rkce_hw); 327 328 void rkce_irq_handler(void *rkce_hw); 329 330 void rkce_irq_thread(void *rkce_hw); 331 332 int rkce_irq_callback_set(void *rkce_hw, enum rkce_td_type td_type, request_cb_func cb_func); 333 334 int rkce_soft_reset(void *rkce_hw, uint32_t reset_sel); 335 336 int rkce_push_td(void *rkce_hw, void *td); 337 338 int rkce_push_td_sync(void *rkce_hw, void *td, uint32_t timeout_ms); 339 340 uint32_t rkce_get_td_type(void *td_buf); 341 342 int rkce_init_symm_td(struct rkce_symm_td *td, struct rkce_symm_td_buf *buf); 343 344 int rkce_init_hash_td(struct rkce_hash_td *td, struct rkce_hash_td_buf *buf); 345 346 bool rkce_hw_algo_valid(void *rkce_hw, uint32_t type, uint32_t algo, uint32_t mode); 347 348 uint32_t rkce_get_keytable_addr(void *rkce_hw); 349 350 #endif 351