1 /* 2 * caam descriptor construction helper functions 3 * 4 * Copyright 2008-2012 Freescale Semiconductor, Inc. 5 */ 6 7 #include "desc.h" 8 #include "regs.h" 9 10 #define IMMEDIATE (1 << 23) 11 #define CAAM_CMD_SZ sizeof(u32) 12 #define CAAM_PTR_SZ sizeof(dma_addr_t) 13 #define CAAM_DESC_BYTES_MAX (CAAM_CMD_SZ * MAX_CAAM_DESCSIZE) 14 #define DESC_JOB_IO_LEN (CAAM_CMD_SZ * 5 + CAAM_PTR_SZ * 3) 15 16 #ifdef DEBUG 17 #define PRINT_POS do { printk(KERN_DEBUG "%02d: %s\n", desc_len(desc),\ 18 &__func__[sizeof("append")]); } while (0) 19 #else 20 #define PRINT_POS 21 #endif 22 23 #define SET_OK_NO_PROP_ERRORS (IMMEDIATE | LDST_CLASS_DECO | \ 24 LDST_SRCDST_WORD_DECOCTRL | \ 25 (LDOFF_CHG_SHARE_OK_NO_PROP << \ 26 LDST_OFFSET_SHIFT)) 27 #define DISABLE_AUTO_INFO_FIFO (IMMEDIATE | LDST_CLASS_DECO | \ 28 LDST_SRCDST_WORD_DECOCTRL | \ 29 (LDOFF_DISABLE_AUTO_NFIFO << LDST_OFFSET_SHIFT)) 30 #define ENABLE_AUTO_INFO_FIFO (IMMEDIATE | LDST_CLASS_DECO | \ 31 LDST_SRCDST_WORD_DECOCTRL | \ 32 (LDOFF_ENABLE_AUTO_NFIFO << LDST_OFFSET_SHIFT)) 33 34 extern bool caam_little_end; 35 36 static inline int desc_len(u32 * const desc) 37 { 38 return caam32_to_cpu(*desc) & HDR_DESCLEN_MASK; 39 } 40 41 static inline int desc_bytes(void * const desc) 42 { 43 return desc_len(desc) * CAAM_CMD_SZ; 44 } 45 46 static inline u32 *desc_end(u32 * const desc) 47 { 48 return desc + desc_len(desc); 49 } 50 51 static inline void *sh_desc_pdb(u32 * const desc) 52 { 53 return desc + 1; 54 } 55 56 static inline void init_desc(u32 * const desc, u32 options) 57 { 58 *desc = cpu_to_caam32((options | HDR_ONE) + 1); 59 } 60 61 static inline void init_sh_desc(u32 * const desc, u32 options) 62 { 63 PRINT_POS; 64 init_desc(desc, CMD_SHARED_DESC_HDR | options); 65 } 66 67 static inline void init_sh_desc_pdb(u32 * const desc, u32 options, 68 size_t pdb_bytes) 69 { 70 u32 pdb_len = (pdb_bytes + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ; 71 72 init_sh_desc(desc, (((pdb_len + 1) << HDR_START_IDX_SHIFT) + pdb_len) | 73 options); 74 } 75 76 static inline void init_job_desc(u32 * const desc, u32 options) 77 { 78 init_desc(desc, CMD_DESC_HDR | options); 79 } 80 81 static inline void init_job_desc_pdb(u32 * const desc, u32 options, 82 size_t pdb_bytes) 83 { 84 u32 pdb_len = (pdb_bytes + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ; 85 86 init_job_desc(desc, (((pdb_len + 1) << HDR_START_IDX_SHIFT)) | options); 87 } 88 89 static inline void append_ptr(u32 * const desc, dma_addr_t ptr) 90 { 91 dma_addr_t *offset = (dma_addr_t *)desc_end(desc); 92 93 *offset = cpu_to_caam_dma(ptr); 94 95 (*desc) = cpu_to_caam32(caam32_to_cpu(*desc) + 96 CAAM_PTR_SZ / CAAM_CMD_SZ); 97 } 98 99 static inline void init_job_desc_shared(u32 * const desc, dma_addr_t ptr, 100 int len, u32 options) 101 { 102 PRINT_POS; 103 init_job_desc(desc, HDR_SHARED | options | 104 (len << HDR_START_IDX_SHIFT)); 105 append_ptr(desc, ptr); 106 } 107 108 static inline void append_data(u32 * const desc, void *data, int len) 109 { 110 u32 *offset = desc_end(desc); 111 112 if (len) /* avoid sparse warning: memcpy with byte count of 0 */ 113 memcpy(offset, data, len); 114 115 (*desc) = cpu_to_caam32(caam32_to_cpu(*desc) + 116 (len + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ); 117 } 118 119 static inline void append_cmd(u32 * const desc, u32 command) 120 { 121 u32 *cmd = desc_end(desc); 122 123 *cmd = cpu_to_caam32(command); 124 125 (*desc) = cpu_to_caam32(caam32_to_cpu(*desc) + 1); 126 } 127 128 #define append_u32 append_cmd 129 130 static inline void append_u64(u32 * const desc, u64 data) 131 { 132 u32 *offset = desc_end(desc); 133 134 /* Only 32-bit alignment is guaranteed in descriptor buffer */ 135 if (caam_little_end) { 136 *offset = cpu_to_caam32(lower_32_bits(data)); 137 *(++offset) = cpu_to_caam32(upper_32_bits(data)); 138 } else { 139 *offset = cpu_to_caam32(upper_32_bits(data)); 140 *(++offset) = cpu_to_caam32(lower_32_bits(data)); 141 } 142 143 (*desc) = cpu_to_caam32(caam32_to_cpu(*desc) + 2); 144 } 145 146 /* Write command without affecting header, and return pointer to next word */ 147 static inline u32 *write_cmd(u32 * const desc, u32 command) 148 { 149 *desc = cpu_to_caam32(command); 150 151 return desc + 1; 152 } 153 154 static inline void append_cmd_ptr(u32 * const desc, dma_addr_t ptr, int len, 155 u32 command) 156 { 157 append_cmd(desc, command | len); 158 append_ptr(desc, ptr); 159 } 160 161 /* Write length after pointer, rather than inside command */ 162 static inline void append_cmd_ptr_extlen(u32 * const desc, dma_addr_t ptr, 163 unsigned int len, u32 command) 164 { 165 append_cmd(desc, command); 166 if (!(command & (SQIN_RTO | SQIN_PRE))) 167 append_ptr(desc, ptr); 168 append_cmd(desc, len); 169 } 170 171 static inline void append_cmd_data(u32 * const desc, void *data, int len, 172 u32 command) 173 { 174 append_cmd(desc, command | IMMEDIATE | len); 175 append_data(desc, data, len); 176 } 177 178 #define APPEND_CMD_RET(cmd, op) \ 179 static inline u32 *append_##cmd(u32 * const desc, u32 options) \ 180 { \ 181 u32 *cmd = desc_end(desc); \ 182 PRINT_POS; \ 183 append_cmd(desc, CMD_##op | options); \ 184 return cmd; \ 185 } 186 APPEND_CMD_RET(jump, JUMP) 187 APPEND_CMD_RET(move, MOVE) 188 189 static inline void set_jump_tgt_here(u32 * const desc, u32 *jump_cmd) 190 { 191 *jump_cmd = cpu_to_caam32(caam32_to_cpu(*jump_cmd) | 192 (desc_len(desc) - (jump_cmd - desc))); 193 } 194 195 static inline void set_move_tgt_here(u32 * const desc, u32 *move_cmd) 196 { 197 u32 val = caam32_to_cpu(*move_cmd); 198 199 val &= ~MOVE_OFFSET_MASK; 200 val |= (desc_len(desc) << (MOVE_OFFSET_SHIFT + 2)) & MOVE_OFFSET_MASK; 201 *move_cmd = cpu_to_caam32(val); 202 } 203 204 #define APPEND_CMD(cmd, op) \ 205 static inline void append_##cmd(u32 * const desc, u32 options) \ 206 { \ 207 PRINT_POS; \ 208 append_cmd(desc, CMD_##op | options); \ 209 } 210 APPEND_CMD(operation, OPERATION) 211 212 #define APPEND_CMD_LEN(cmd, op) \ 213 static inline void append_##cmd(u32 * const desc, unsigned int len, \ 214 u32 options) \ 215 { \ 216 PRINT_POS; \ 217 append_cmd(desc, CMD_##op | len | options); \ 218 } 219 220 APPEND_CMD_LEN(seq_load, SEQ_LOAD) 221 APPEND_CMD_LEN(seq_store, SEQ_STORE) 222 APPEND_CMD_LEN(seq_fifo_load, SEQ_FIFO_LOAD) 223 APPEND_CMD_LEN(seq_fifo_store, SEQ_FIFO_STORE) 224 225 #define APPEND_CMD_PTR(cmd, op) \ 226 static inline void append_##cmd(u32 * const desc, dma_addr_t ptr, \ 227 unsigned int len, u32 options) \ 228 { \ 229 PRINT_POS; \ 230 append_cmd_ptr(desc, ptr, len, CMD_##op | options); \ 231 } 232 APPEND_CMD_PTR(key, KEY) 233 APPEND_CMD_PTR(load, LOAD) 234 APPEND_CMD_PTR(fifo_load, FIFO_LOAD) 235 APPEND_CMD_PTR(fifo_store, FIFO_STORE) 236 237 static inline void append_store(u32 * const desc, dma_addr_t ptr, 238 unsigned int len, u32 options) 239 { 240 u32 cmd_src; 241 242 cmd_src = options & LDST_SRCDST_MASK; 243 244 append_cmd(desc, CMD_STORE | options | len); 245 246 /* The following options do not require pointer */ 247 if (!(cmd_src == LDST_SRCDST_WORD_DESCBUF_SHARED || 248 cmd_src == LDST_SRCDST_WORD_DESCBUF_JOB || 249 cmd_src == LDST_SRCDST_WORD_DESCBUF_JOB_WE || 250 cmd_src == LDST_SRCDST_WORD_DESCBUF_SHARED_WE)) 251 append_ptr(desc, ptr); 252 } 253 254 #define APPEND_SEQ_PTR_INTLEN(cmd, op) \ 255 static inline void append_seq_##cmd##_ptr_intlen(u32 * const desc, \ 256 dma_addr_t ptr, \ 257 unsigned int len, \ 258 u32 options) \ 259 { \ 260 PRINT_POS; \ 261 if (options & (SQIN_RTO | SQIN_PRE)) \ 262 append_cmd(desc, CMD_SEQ_##op##_PTR | len | options); \ 263 else \ 264 append_cmd_ptr(desc, ptr, len, CMD_SEQ_##op##_PTR | options); \ 265 } 266 APPEND_SEQ_PTR_INTLEN(in, IN) 267 APPEND_SEQ_PTR_INTLEN(out, OUT) 268 269 #define APPEND_CMD_PTR_TO_IMM(cmd, op) \ 270 static inline void append_##cmd##_as_imm(u32 * const desc, void *data, \ 271 unsigned int len, u32 options) \ 272 { \ 273 PRINT_POS; \ 274 append_cmd_data(desc, data, len, CMD_##op | options); \ 275 } 276 APPEND_CMD_PTR_TO_IMM(load, LOAD); 277 APPEND_CMD_PTR_TO_IMM(fifo_load, FIFO_LOAD); 278 279 #define APPEND_CMD_PTR_EXTLEN(cmd, op) \ 280 static inline void append_##cmd##_extlen(u32 * const desc, dma_addr_t ptr, \ 281 unsigned int len, u32 options) \ 282 { \ 283 PRINT_POS; \ 284 append_cmd_ptr_extlen(desc, ptr, len, CMD_##op | SQIN_EXT | options); \ 285 } 286 APPEND_CMD_PTR_EXTLEN(seq_in_ptr, SEQ_IN_PTR) 287 APPEND_CMD_PTR_EXTLEN(seq_out_ptr, SEQ_OUT_PTR) 288 289 /* 290 * Determine whether to store length internally or externally depending on 291 * the size of its type 292 */ 293 #define APPEND_CMD_PTR_LEN(cmd, op, type) \ 294 static inline void append_##cmd(u32 * const desc, dma_addr_t ptr, \ 295 type len, u32 options) \ 296 { \ 297 PRINT_POS; \ 298 if (sizeof(type) > sizeof(u16)) \ 299 append_##cmd##_extlen(desc, ptr, len, options); \ 300 else \ 301 append_##cmd##_intlen(desc, ptr, len, options); \ 302 } 303 APPEND_CMD_PTR_LEN(seq_in_ptr, SEQ_IN_PTR, u32) 304 APPEND_CMD_PTR_LEN(seq_out_ptr, SEQ_OUT_PTR, u32) 305 306 /* 307 * 2nd variant for commands whose specified immediate length differs 308 * from length of immediate data provided, e.g., split keys 309 */ 310 #define APPEND_CMD_PTR_TO_IMM2(cmd, op) \ 311 static inline void append_##cmd##_as_imm(u32 * const desc, void *data, \ 312 unsigned int data_len, \ 313 unsigned int len, u32 options) \ 314 { \ 315 PRINT_POS; \ 316 append_cmd(desc, CMD_##op | IMMEDIATE | len | options); \ 317 append_data(desc, data, data_len); \ 318 } 319 APPEND_CMD_PTR_TO_IMM2(key, KEY); 320 321 #define APPEND_CMD_RAW_IMM(cmd, op, type) \ 322 static inline void append_##cmd##_imm_##type(u32 * const desc, type immediate, \ 323 u32 options) \ 324 { \ 325 PRINT_POS; \ 326 append_cmd(desc, CMD_##op | IMMEDIATE | options | sizeof(type)); \ 327 append_cmd(desc, immediate); \ 328 } 329 APPEND_CMD_RAW_IMM(load, LOAD, u32); 330 331 /* 332 * ee - endianness 333 * size - size of immediate type in bytes 334 */ 335 #define APPEND_CMD_RAW_IMM2(cmd, op, ee, size) \ 336 static inline void append_##cmd##_imm_##ee##size(u32 *desc, \ 337 u##size immediate, \ 338 u32 options) \ 339 { \ 340 __##ee##size data = cpu_to_##ee##size(immediate); \ 341 PRINT_POS; \ 342 append_cmd(desc, CMD_##op | IMMEDIATE | options | sizeof(data)); \ 343 append_data(desc, &data, sizeof(data)); \ 344 } 345 346 APPEND_CMD_RAW_IMM2(load, LOAD, be, 32); 347 348 /* 349 * Append math command. Only the last part of destination and source need to 350 * be specified 351 */ 352 #define APPEND_MATH(op, desc, dest, src_0, src_1, len) \ 353 append_cmd(desc, CMD_MATH | MATH_FUN_##op | MATH_DEST_##dest | \ 354 MATH_SRC0_##src_0 | MATH_SRC1_##src_1 | (u32)len); 355 356 #define append_math_add(desc, dest, src0, src1, len) \ 357 APPEND_MATH(ADD, desc, dest, src0, src1, len) 358 #define append_math_sub(desc, dest, src0, src1, len) \ 359 APPEND_MATH(SUB, desc, dest, src0, src1, len) 360 #define append_math_add_c(desc, dest, src0, src1, len) \ 361 APPEND_MATH(ADDC, desc, dest, src0, src1, len) 362 #define append_math_sub_b(desc, dest, src0, src1, len) \ 363 APPEND_MATH(SUBB, desc, dest, src0, src1, len) 364 #define append_math_and(desc, dest, src0, src1, len) \ 365 APPEND_MATH(AND, desc, dest, src0, src1, len) 366 #define append_math_or(desc, dest, src0, src1, len) \ 367 APPEND_MATH(OR, desc, dest, src0, src1, len) 368 #define append_math_xor(desc, dest, src0, src1, len) \ 369 APPEND_MATH(XOR, desc, dest, src0, src1, len) 370 #define append_math_lshift(desc, dest, src0, src1, len) \ 371 APPEND_MATH(LSHIFT, desc, dest, src0, src1, len) 372 #define append_math_rshift(desc, dest, src0, src1, len) \ 373 APPEND_MATH(RSHIFT, desc, dest, src0, src1, len) 374 #define append_math_ldshift(desc, dest, src0, src1, len) \ 375 APPEND_MATH(SHLD, desc, dest, src0, src1, len) 376 377 /* Exactly one source is IMM. Data is passed in as u32 value */ 378 #define APPEND_MATH_IMM_u32(op, desc, dest, src_0, src_1, data) \ 379 do { \ 380 APPEND_MATH(op, desc, dest, src_0, src_1, CAAM_CMD_SZ); \ 381 append_cmd(desc, data); \ 382 } while (0) 383 384 #define append_math_add_imm_u32(desc, dest, src0, src1, data) \ 385 APPEND_MATH_IMM_u32(ADD, desc, dest, src0, src1, data) 386 #define append_math_sub_imm_u32(desc, dest, src0, src1, data) \ 387 APPEND_MATH_IMM_u32(SUB, desc, dest, src0, src1, data) 388 #define append_math_add_c_imm_u32(desc, dest, src0, src1, data) \ 389 APPEND_MATH_IMM_u32(ADDC, desc, dest, src0, src1, data) 390 #define append_math_sub_b_imm_u32(desc, dest, src0, src1, data) \ 391 APPEND_MATH_IMM_u32(SUBB, desc, dest, src0, src1, data) 392 #define append_math_and_imm_u32(desc, dest, src0, src1, data) \ 393 APPEND_MATH_IMM_u32(AND, desc, dest, src0, src1, data) 394 #define append_math_or_imm_u32(desc, dest, src0, src1, data) \ 395 APPEND_MATH_IMM_u32(OR, desc, dest, src0, src1, data) 396 #define append_math_xor_imm_u32(desc, dest, src0, src1, data) \ 397 APPEND_MATH_IMM_u32(XOR, desc, dest, src0, src1, data) 398 #define append_math_lshift_imm_u32(desc, dest, src0, src1, data) \ 399 APPEND_MATH_IMM_u32(LSHIFT, desc, dest, src0, src1, data) 400 #define append_math_rshift_imm_u32(desc, dest, src0, src1, data) \ 401 APPEND_MATH_IMM_u32(RSHIFT, desc, dest, src0, src1, data) 402 403 /* Exactly one source is IMM. Data is passed in as u64 value */ 404 #define APPEND_MATH_IMM_u64(op, desc, dest, src_0, src_1, data) \ 405 do { \ 406 u32 upper = (data >> 16) >> 16; \ 407 APPEND_MATH(op, desc, dest, src_0, src_1, CAAM_CMD_SZ * 2 | \ 408 (upper ? 0 : MATH_IFB)); \ 409 if (upper) \ 410 append_u64(desc, data); \ 411 else \ 412 append_u32(desc, lower_32_bits(data)); \ 413 } while (0) 414 415 #define append_math_add_imm_u64(desc, dest, src0, src1, data) \ 416 APPEND_MATH_IMM_u64(ADD, desc, dest, src0, src1, data) 417 #define append_math_sub_imm_u64(desc, dest, src0, src1, data) \ 418 APPEND_MATH_IMM_u64(SUB, desc, dest, src0, src1, data) 419 #define append_math_add_c_imm_u64(desc, dest, src0, src1, data) \ 420 APPEND_MATH_IMM_u64(ADDC, desc, dest, src0, src1, data) 421 #define append_math_sub_b_imm_u64(desc, dest, src0, src1, data) \ 422 APPEND_MATH_IMM_u64(SUBB, desc, dest, src0, src1, data) 423 #define append_math_and_imm_u64(desc, dest, src0, src1, data) \ 424 APPEND_MATH_IMM_u64(AND, desc, dest, src0, src1, data) 425 #define append_math_or_imm_u64(desc, dest, src0, src1, data) \ 426 APPEND_MATH_IMM_u64(OR, desc, dest, src0, src1, data) 427 #define append_math_xor_imm_u64(desc, dest, src0, src1, data) \ 428 APPEND_MATH_IMM_u64(XOR, desc, dest, src0, src1, data) 429 #define append_math_lshift_imm_u64(desc, dest, src0, src1, data) \ 430 APPEND_MATH_IMM_u64(LSHIFT, desc, dest, src0, src1, data) 431 #define append_math_rshift_imm_u64(desc, dest, src0, src1, data) \ 432 APPEND_MATH_IMM_u64(RSHIFT, desc, dest, src0, src1, data) 433 434 /** 435 * struct alginfo - Container for algorithm details 436 * @algtype: algorithm selector; for valid values, see documentation of the 437 * functions where it is used. 438 * @keylen: length of the provided algorithm key, in bytes 439 * @keylen_pad: padded length of the provided algorithm key, in bytes 440 * @key: address where algorithm key resides; virtual address if key_inline 441 * is true, dma (bus) address if key_inline is false. 442 * @key_inline: true - key can be inlined in the descriptor; false - key is 443 * referenced by the descriptor 444 */ 445 struct alginfo { 446 u32 algtype; 447 unsigned int keylen; 448 unsigned int keylen_pad; 449 union { 450 dma_addr_t key_dma; 451 void *key_virt; 452 }; 453 bool key_inline; 454 }; 455 456 /** 457 * desc_inline_query() - Provide indications on which data items can be inlined 458 * and which shall be referenced in a shared descriptor. 459 * @sd_base_len: Shared descriptor base length - bytes consumed by the commands, 460 * excluding the data items to be inlined (or corresponding 461 * pointer if an item is not inlined). Each cnstr_* function that 462 * generates descriptors should have a define mentioning 463 * corresponding length. 464 * @jd_len: Maximum length of the job descriptor(s) that will be used 465 * together with the shared descriptor. 466 * @data_len: Array of lengths of the data items trying to be inlined 467 * @inl_mask: 32bit mask with bit x = 1 if data item x can be inlined, 0 468 * otherwise. 469 * @count: Number of data items (size of @data_len array); must be <= 32 470 * 471 * Return: 0 if data can be inlined / referenced, negative value if not. If 0, 472 * check @inl_mask for details. 473 */ 474 static inline int desc_inline_query(unsigned int sd_base_len, 475 unsigned int jd_len, unsigned int *data_len, 476 u32 *inl_mask, unsigned int count) 477 { 478 int rem_bytes = (int)(CAAM_DESC_BYTES_MAX - sd_base_len - jd_len); 479 unsigned int i; 480 481 *inl_mask = 0; 482 for (i = 0; (i < count) && (rem_bytes > 0); i++) { 483 if (rem_bytes - (int)(data_len[i] + 484 (count - i - 1) * CAAM_PTR_SZ) >= 0) { 485 rem_bytes -= data_len[i]; 486 *inl_mask |= (1 << i); 487 } else { 488 rem_bytes -= CAAM_PTR_SZ; 489 } 490 } 491 492 return (rem_bytes >= 0) ? 0 : -1; 493 } 494