1 /* 2 * caam descriptor construction helper functions 3 * 4 * Copyright 2008-2012 Freescale Semiconductor, Inc. 5 */ 6 7 #include "desc.h" 8 9 #define IMMEDIATE (1 << 23) 10 #define CAAM_CMD_SZ sizeof(u32) 11 #define CAAM_PTR_SZ sizeof(dma_addr_t) 12 #define CAAM_DESC_BYTES_MAX (CAAM_CMD_SZ * MAX_CAAM_DESCSIZE) 13 #define DESC_JOB_IO_LEN (CAAM_CMD_SZ * 5 + CAAM_PTR_SZ * 3) 14 15 #ifdef DEBUG 16 #define PRINT_POS do { printk(KERN_DEBUG "%02d: %s\n", desc_len(desc),\ 17 &__func__[sizeof("append")]); } while (0) 18 #else 19 #define PRINT_POS 20 #endif 21 22 #define SET_OK_NO_PROP_ERRORS (IMMEDIATE | LDST_CLASS_DECO | \ 23 LDST_SRCDST_WORD_DECOCTRL | \ 24 (LDOFF_CHG_SHARE_OK_NO_PROP << \ 25 LDST_OFFSET_SHIFT)) 26 #define DISABLE_AUTO_INFO_FIFO (IMMEDIATE | LDST_CLASS_DECO | \ 27 LDST_SRCDST_WORD_DECOCTRL | \ 28 (LDOFF_DISABLE_AUTO_NFIFO << LDST_OFFSET_SHIFT)) 29 #define ENABLE_AUTO_INFO_FIFO (IMMEDIATE | LDST_CLASS_DECO | \ 30 LDST_SRCDST_WORD_DECOCTRL | \ 31 (LDOFF_ENABLE_AUTO_NFIFO << LDST_OFFSET_SHIFT)) 32 33 static inline int desc_len(u32 *desc) 34 { 35 return *desc & HDR_DESCLEN_MASK; 36 } 37 38 static inline int desc_bytes(void *desc) 39 { 40 return desc_len(desc) * CAAM_CMD_SZ; 41 } 42 43 static inline u32 *desc_end(u32 *desc) 44 { 45 return desc + desc_len(desc); 46 } 47 48 static inline void *sh_desc_pdb(u32 *desc) 49 { 50 return desc + 1; 51 } 52 53 static inline void init_desc(u32 *desc, u32 options) 54 { 55 *desc = (options | HDR_ONE) + 1; 56 } 57 58 static inline void init_sh_desc(u32 *desc, u32 options) 59 { 60 PRINT_POS; 61 init_desc(desc, CMD_SHARED_DESC_HDR | options); 62 } 63 64 static inline void init_sh_desc_pdb(u32 *desc, u32 options, size_t pdb_bytes) 65 { 66 u32 pdb_len = (pdb_bytes + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ; 67 68 init_sh_desc(desc, (((pdb_len + 1) << HDR_START_IDX_SHIFT) + pdb_len) | 69 options); 70 } 71 72 static inline void init_job_desc(u32 *desc, u32 options) 73 { 74 init_desc(desc, CMD_DESC_HDR | options); 75 } 76 77 static inline void append_ptr(u32 *desc, dma_addr_t ptr) 78 { 79 dma_addr_t *offset = (dma_addr_t *)desc_end(desc); 80 81 *offset = ptr; 82 83 (*desc) += CAAM_PTR_SZ / CAAM_CMD_SZ; 84 } 85 86 static inline void init_job_desc_shared(u32 *desc, dma_addr_t ptr, int len, 87 u32 options) 88 { 89 PRINT_POS; 90 init_job_desc(desc, HDR_SHARED | options | 91 (len << HDR_START_IDX_SHIFT)); 92 append_ptr(desc, ptr); 93 } 94 95 static inline void append_data(u32 *desc, void *data, int len) 96 { 97 u32 *offset = desc_end(desc); 98 99 if (len) /* avoid sparse warning: memcpy with byte count of 0 */ 100 memcpy(offset, data, len); 101 102 (*desc) += (len + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ; 103 } 104 105 static inline void append_cmd(u32 *desc, u32 command) 106 { 107 u32 *cmd = desc_end(desc); 108 109 *cmd = command; 110 111 (*desc)++; 112 } 113 114 #define append_u32 append_cmd 115 116 static inline void append_u64(u32 *desc, u64 data) 117 { 118 u32 *offset = desc_end(desc); 119 120 *offset = upper_32_bits(data); 121 *(++offset) = lower_32_bits(data); 122 123 (*desc) += 2; 124 } 125 126 /* Write command without affecting header, and return pointer to next word */ 127 static inline u32 *write_cmd(u32 *desc, u32 command) 128 { 129 *desc = command; 130 131 return desc + 1; 132 } 133 134 static inline void append_cmd_ptr(u32 *desc, dma_addr_t ptr, int len, 135 u32 command) 136 { 137 append_cmd(desc, command | len); 138 append_ptr(desc, ptr); 139 } 140 141 /* Write length after pointer, rather than inside command */ 142 static inline void append_cmd_ptr_extlen(u32 *desc, dma_addr_t ptr, 143 unsigned int len, u32 command) 144 { 145 append_cmd(desc, command); 146 if (!(command & (SQIN_RTO | SQIN_PRE))) 147 append_ptr(desc, ptr); 148 append_cmd(desc, len); 149 } 150 151 static inline void append_cmd_data(u32 *desc, void *data, int len, 152 u32 command) 153 { 154 append_cmd(desc, command | IMMEDIATE | len); 155 append_data(desc, data, len); 156 } 157 158 #define APPEND_CMD_RET(cmd, op) \ 159 static inline u32 *append_##cmd(u32 *desc, u32 options) \ 160 { \ 161 u32 *cmd = desc_end(desc); \ 162 PRINT_POS; \ 163 append_cmd(desc, CMD_##op | options); \ 164 return cmd; \ 165 } 166 APPEND_CMD_RET(jump, JUMP) 167 APPEND_CMD_RET(move, MOVE) 168 169 static inline void set_jump_tgt_here(u32 *desc, u32 *jump_cmd) 170 { 171 *jump_cmd = *jump_cmd | (desc_len(desc) - (jump_cmd - desc)); 172 } 173 174 static inline void set_move_tgt_here(u32 *desc, u32 *move_cmd) 175 { 176 *move_cmd &= ~MOVE_OFFSET_MASK; 177 *move_cmd = *move_cmd | ((desc_len(desc) << (MOVE_OFFSET_SHIFT + 2)) & 178 MOVE_OFFSET_MASK); 179 } 180 181 #define APPEND_CMD(cmd, op) \ 182 static inline void append_##cmd(u32 *desc, u32 options) \ 183 { \ 184 PRINT_POS; \ 185 append_cmd(desc, CMD_##op | options); \ 186 } 187 APPEND_CMD(operation, OPERATION) 188 189 #define APPEND_CMD_LEN(cmd, op) \ 190 static inline void append_##cmd(u32 *desc, unsigned int len, u32 options) \ 191 { \ 192 PRINT_POS; \ 193 append_cmd(desc, CMD_##op | len | options); \ 194 } 195 APPEND_CMD_LEN(seq_store, SEQ_STORE) 196 APPEND_CMD_LEN(seq_fifo_load, SEQ_FIFO_LOAD) 197 APPEND_CMD_LEN(seq_fifo_store, SEQ_FIFO_STORE) 198 199 #define APPEND_CMD_PTR(cmd, op) \ 200 static inline void append_##cmd(u32 *desc, dma_addr_t ptr, unsigned int len, \ 201 u32 options) \ 202 { \ 203 PRINT_POS; \ 204 append_cmd_ptr(desc, ptr, len, CMD_##op | options); \ 205 } 206 APPEND_CMD_PTR(key, KEY) 207 APPEND_CMD_PTR(load, LOAD) 208 APPEND_CMD_PTR(fifo_load, FIFO_LOAD) 209 APPEND_CMD_PTR(fifo_store, FIFO_STORE) 210 211 static inline void append_store(u32 *desc, dma_addr_t ptr, unsigned int len, 212 u32 options) 213 { 214 u32 cmd_src; 215 216 cmd_src = options & LDST_SRCDST_MASK; 217 218 append_cmd(desc, CMD_STORE | options | len); 219 220 /* The following options do not require pointer */ 221 if (!(cmd_src == LDST_SRCDST_WORD_DESCBUF_SHARED || 222 cmd_src == LDST_SRCDST_WORD_DESCBUF_JOB || 223 cmd_src == LDST_SRCDST_WORD_DESCBUF_JOB_WE || 224 cmd_src == LDST_SRCDST_WORD_DESCBUF_SHARED_WE)) 225 append_ptr(desc, ptr); 226 } 227 228 #define APPEND_SEQ_PTR_INTLEN(cmd, op) \ 229 static inline void append_seq_##cmd##_ptr_intlen(u32 *desc, dma_addr_t ptr, \ 230 unsigned int len, \ 231 u32 options) \ 232 { \ 233 PRINT_POS; \ 234 if (options & (SQIN_RTO | SQIN_PRE)) \ 235 append_cmd(desc, CMD_SEQ_##op##_PTR | len | options); \ 236 else \ 237 append_cmd_ptr(desc, ptr, len, CMD_SEQ_##op##_PTR | options); \ 238 } 239 APPEND_SEQ_PTR_INTLEN(in, IN) 240 APPEND_SEQ_PTR_INTLEN(out, OUT) 241 242 #define APPEND_CMD_PTR_TO_IMM(cmd, op) \ 243 static inline void append_##cmd##_as_imm(u32 *desc, void *data, \ 244 unsigned int len, u32 options) \ 245 { \ 246 PRINT_POS; \ 247 append_cmd_data(desc, data, len, CMD_##op | options); \ 248 } 249 APPEND_CMD_PTR_TO_IMM(load, LOAD); 250 APPEND_CMD_PTR_TO_IMM(fifo_load, FIFO_LOAD); 251 252 #define APPEND_CMD_PTR_EXTLEN(cmd, op) \ 253 static inline void append_##cmd##_extlen(u32 *desc, dma_addr_t ptr, \ 254 unsigned int len, u32 options) \ 255 { \ 256 PRINT_POS; \ 257 append_cmd_ptr_extlen(desc, ptr, len, CMD_##op | SQIN_EXT | options); \ 258 } 259 APPEND_CMD_PTR_EXTLEN(seq_in_ptr, SEQ_IN_PTR) 260 APPEND_CMD_PTR_EXTLEN(seq_out_ptr, SEQ_OUT_PTR) 261 262 /* 263 * Determine whether to store length internally or externally depending on 264 * the size of its type 265 */ 266 #define APPEND_CMD_PTR_LEN(cmd, op, type) \ 267 static inline void append_##cmd(u32 *desc, dma_addr_t ptr, \ 268 type len, u32 options) \ 269 { \ 270 PRINT_POS; \ 271 if (sizeof(type) > sizeof(u16)) \ 272 append_##cmd##_extlen(desc, ptr, len, options); \ 273 else \ 274 append_##cmd##_intlen(desc, ptr, len, options); \ 275 } 276 APPEND_CMD_PTR_LEN(seq_in_ptr, SEQ_IN_PTR, u32) 277 APPEND_CMD_PTR_LEN(seq_out_ptr, SEQ_OUT_PTR, u32) 278 279 /* 280 * 2nd variant for commands whose specified immediate length differs 281 * from length of immediate data provided, e.g., split keys 282 */ 283 #define APPEND_CMD_PTR_TO_IMM2(cmd, op) \ 284 static inline void append_##cmd##_as_imm(u32 *desc, void *data, \ 285 unsigned int data_len, \ 286 unsigned int len, u32 options) \ 287 { \ 288 PRINT_POS; \ 289 append_cmd(desc, CMD_##op | IMMEDIATE | len | options); \ 290 append_data(desc, data, data_len); \ 291 } 292 APPEND_CMD_PTR_TO_IMM2(key, KEY); 293 294 #define APPEND_CMD_RAW_IMM(cmd, op, type) \ 295 static inline void append_##cmd##_imm_##type(u32 *desc, type immediate, \ 296 u32 options) \ 297 { \ 298 PRINT_POS; \ 299 append_cmd(desc, CMD_##op | IMMEDIATE | options | sizeof(type)); \ 300 append_cmd(desc, immediate); \ 301 } 302 APPEND_CMD_RAW_IMM(load, LOAD, u32); 303 304 /* 305 * Append math command. Only the last part of destination and source need to 306 * be specified 307 */ 308 #define APPEND_MATH(op, desc, dest, src_0, src_1, len) \ 309 append_cmd(desc, CMD_MATH | MATH_FUN_##op | MATH_DEST_##dest | \ 310 MATH_SRC0_##src_0 | MATH_SRC1_##src_1 | (u32)len); 311 312 #define append_math_add(desc, dest, src0, src1, len) \ 313 APPEND_MATH(ADD, desc, dest, src0, src1, len) 314 #define append_math_sub(desc, dest, src0, src1, len) \ 315 APPEND_MATH(SUB, desc, dest, src0, src1, len) 316 #define append_math_add_c(desc, dest, src0, src1, len) \ 317 APPEND_MATH(ADDC, desc, dest, src0, src1, len) 318 #define append_math_sub_b(desc, dest, src0, src1, len) \ 319 APPEND_MATH(SUBB, desc, dest, src0, src1, len) 320 #define append_math_and(desc, dest, src0, src1, len) \ 321 APPEND_MATH(AND, desc, dest, src0, src1, len) 322 #define append_math_or(desc, dest, src0, src1, len) \ 323 APPEND_MATH(OR, desc, dest, src0, src1, len) 324 #define append_math_xor(desc, dest, src0, src1, len) \ 325 APPEND_MATH(XOR, desc, dest, src0, src1, len) 326 #define append_math_lshift(desc, dest, src0, src1, len) \ 327 APPEND_MATH(LSHIFT, desc, dest, src0, src1, len) 328 #define append_math_rshift(desc, dest, src0, src1, len) \ 329 APPEND_MATH(RSHIFT, desc, dest, src0, src1, len) 330 #define append_math_ldshift(desc, dest, src0, src1, len) \ 331 APPEND_MATH(SHLD, desc, dest, src0, src1, len) 332 333 /* Exactly one source is IMM. Data is passed in as u32 value */ 334 #define APPEND_MATH_IMM_u32(op, desc, dest, src_0, src_1, data) \ 335 do { \ 336 APPEND_MATH(op, desc, dest, src_0, src_1, CAAM_CMD_SZ); \ 337 append_cmd(desc, data); \ 338 } while (0) 339 340 #define append_math_add_imm_u32(desc, dest, src0, src1, data) \ 341 APPEND_MATH_IMM_u32(ADD, desc, dest, src0, src1, data) 342 #define append_math_sub_imm_u32(desc, dest, src0, src1, data) \ 343 APPEND_MATH_IMM_u32(SUB, desc, dest, src0, src1, data) 344 #define append_math_add_c_imm_u32(desc, dest, src0, src1, data) \ 345 APPEND_MATH_IMM_u32(ADDC, desc, dest, src0, src1, data) 346 #define append_math_sub_b_imm_u32(desc, dest, src0, src1, data) \ 347 APPEND_MATH_IMM_u32(SUBB, desc, dest, src0, src1, data) 348 #define append_math_and_imm_u32(desc, dest, src0, src1, data) \ 349 APPEND_MATH_IMM_u32(AND, desc, dest, src0, src1, data) 350 #define append_math_or_imm_u32(desc, dest, src0, src1, data) \ 351 APPEND_MATH_IMM_u32(OR, desc, dest, src0, src1, data) 352 #define append_math_xor_imm_u32(desc, dest, src0, src1, data) \ 353 APPEND_MATH_IMM_u32(XOR, desc, dest, src0, src1, data) 354 #define append_math_lshift_imm_u32(desc, dest, src0, src1, data) \ 355 APPEND_MATH_IMM_u32(LSHIFT, desc, dest, src0, src1, data) 356 #define append_math_rshift_imm_u32(desc, dest, src0, src1, data) \ 357 APPEND_MATH_IMM_u32(RSHIFT, desc, dest, src0, src1, data) 358 359 /* Exactly one source is IMM. Data is passed in as u64 value */ 360 #define APPEND_MATH_IMM_u64(op, desc, dest, src_0, src_1, data) \ 361 do { \ 362 u32 upper = (data >> 16) >> 16; \ 363 APPEND_MATH(op, desc, dest, src_0, src_1, CAAM_CMD_SZ * 2 | \ 364 (upper ? 0 : MATH_IFB)); \ 365 if (upper) \ 366 append_u64(desc, data); \ 367 else \ 368 append_u32(desc, data); \ 369 } while (0) 370 371 #define append_math_add_imm_u64(desc, dest, src0, src1, data) \ 372 APPEND_MATH_IMM_u64(ADD, desc, dest, src0, src1, data) 373 #define append_math_sub_imm_u64(desc, dest, src0, src1, data) \ 374 APPEND_MATH_IMM_u64(SUB, desc, dest, src0, src1, data) 375 #define append_math_add_c_imm_u64(desc, dest, src0, src1, data) \ 376 APPEND_MATH_IMM_u64(ADDC, desc, dest, src0, src1, data) 377 #define append_math_sub_b_imm_u64(desc, dest, src0, src1, data) \ 378 APPEND_MATH_IMM_u64(SUBB, desc, dest, src0, src1, data) 379 #define append_math_and_imm_u64(desc, dest, src0, src1, data) \ 380 APPEND_MATH_IMM_u64(AND, desc, dest, src0, src1, data) 381 #define append_math_or_imm_u64(desc, dest, src0, src1, data) \ 382 APPEND_MATH_IMM_u64(OR, desc, dest, src0, src1, data) 383 #define append_math_xor_imm_u64(desc, dest, src0, src1, data) \ 384 APPEND_MATH_IMM_u64(XOR, desc, dest, src0, src1, data) 385 #define append_math_lshift_imm_u64(desc, dest, src0, src1, data) \ 386 APPEND_MATH_IMM_u64(LSHIFT, desc, dest, src0, src1, data) 387 #define append_math_rshift_imm_u64(desc, dest, src0, src1, data) \ 388 APPEND_MATH_IMM_u64(RSHIFT, desc, dest, src0, src1, data) 389