Ruchika Gupta | b9eebfa | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 1 | /* |
| 2 | * caam descriptor construction helper functions |
| 3 | * |
| 4 | * Copyright 2008-2014 Freescale Semiconductor, Inc. |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | * |
| 8 | * Based on desc_constr.h file in linux drivers/crypto/caam |
| 9 | */ |
| 10 | |
| 11 | #include <linux/compat.h> |
| 12 | #include "desc.h" |
| 13 | |
| 14 | #define IMMEDIATE (1 << 23) |
| 15 | #define CAAM_CMD_SZ sizeof(u32) |
| 16 | #define CAAM_PTR_SZ sizeof(dma_addr_t) |
| 17 | #define CAAM_DESC_BYTES_MAX (CAAM_CMD_SZ * MAX_CAAM_DESCSIZE) |
| 18 | #define DESC_JOB_IO_LEN (CAAM_CMD_SZ * 5 + CAAM_PTR_SZ * 3) |
| 19 | |
| 20 | #ifdef DEBUG |
| 21 | #define PRINT_POS do { printf("%02d: %s\n", desc_len(desc),\ |
| 22 | &__func__[sizeof("append")]); \ |
| 23 | } while (0) |
| 24 | #else |
| 25 | #define PRINT_POS |
| 26 | #endif |
| 27 | |
| 28 | #define SET_OK_NO_PROP_ERRORS (IMMEDIATE | LDST_CLASS_DECO | \ |
| 29 | LDST_SRCDST_WORD_DECOCTRL | \ |
| 30 | (LDOFF_CHG_SHARE_OK_NO_PROP << \ |
| 31 | LDST_OFFSET_SHIFT)) |
| 32 | #define DISABLE_AUTO_INFO_FIFO (IMMEDIATE | LDST_CLASS_DECO | \ |
| 33 | LDST_SRCDST_WORD_DECOCTRL | \ |
| 34 | (LDOFF_DISABLE_AUTO_NFIFO << LDST_OFFSET_SHIFT)) |
| 35 | #define ENABLE_AUTO_INFO_FIFO (IMMEDIATE | LDST_CLASS_DECO | \ |
| 36 | LDST_SRCDST_WORD_DECOCTRL | \ |
| 37 | (LDOFF_ENABLE_AUTO_NFIFO << LDST_OFFSET_SHIFT)) |
| 38 | |
Aneesh Bansal | f59e69c | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 39 | #ifdef CONFIG_PHYS_64BIT |
| 40 | union ptr_addr_t { |
| 41 | u64 m_whole; |
| 42 | struct { |
| 43 | #ifdef CONFIG_SYS_FSL_SEC_LE |
| 44 | u32 low; |
| 45 | u32 high; |
| 46 | #elif defined(CONFIG_SYS_FSL_SEC_BE) |
| 47 | u32 high; |
| 48 | u32 low; |
| 49 | #else |
| 50 | #error Neither CONFIG_SYS_FSL_SEC_LE nor CONFIG_SYS_FSL_SEC_BE is defined |
| 51 | #endif |
| 52 | } m_halfs; |
| 53 | }; |
| 54 | #endif |
| 55 | |
Ruchika Gupta | b9eebfa | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 56 | static inline int desc_len(u32 *desc) |
| 57 | { |
| 58 | return *desc & HDR_DESCLEN_MASK; |
| 59 | } |
| 60 | |
| 61 | static inline int desc_bytes(void *desc) |
| 62 | { |
| 63 | return desc_len(desc) * CAAM_CMD_SZ; |
| 64 | } |
| 65 | |
| 66 | static inline u32 *desc_end(u32 *desc) |
| 67 | { |
| 68 | return desc + desc_len(desc); |
| 69 | } |
| 70 | |
| 71 | static inline void init_desc(u32 *desc, u32 options) |
| 72 | { |
| 73 | *desc = (options | HDR_ONE) + 1; |
| 74 | } |
| 75 | |
| 76 | static inline void init_job_desc(u32 *desc, u32 options) |
| 77 | { |
| 78 | init_desc(desc, CMD_DESC_HDR | options); |
| 79 | } |
| 80 | |
| 81 | static inline void append_ptr(u32 *desc, dma_addr_t ptr) |
| 82 | { |
| 83 | dma_addr_t *offset = (dma_addr_t *)desc_end(desc); |
| 84 | |
Aneesh Bansal | f59e69c | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 85 | #ifdef CONFIG_PHYS_64BIT |
| 86 | /* The Position of low and high part of 64 bit address |
| 87 | * will depend on the endianness of CAAM Block */ |
| 88 | union ptr_addr_t ptr_addr; |
| 89 | ptr_addr.m_halfs.high = (u32)(ptr >> 32); |
| 90 | ptr_addr.m_halfs.low = (u32)ptr; |
| 91 | *offset = ptr_addr.m_whole; |
| 92 | #else |
Ruchika Gupta | b9eebfa | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 93 | *offset = ptr; |
Aneesh Bansal | f59e69c | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 94 | #endif |
Ruchika Gupta | b9eebfa | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 95 | |
| 96 | (*desc) += CAAM_PTR_SZ / CAAM_CMD_SZ; |
| 97 | } |
| 98 | |
| 99 | static inline void append_data(u32 *desc, void *data, int len) |
| 100 | { |
| 101 | u32 *offset = desc_end(desc); |
| 102 | |
| 103 | if (len) /* avoid sparse warning: memcpy with byte count of 0 */ |
| 104 | memcpy(offset, data, len); |
| 105 | |
| 106 | (*desc) += (len + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ; |
| 107 | } |
| 108 | |
| 109 | static inline void append_cmd(u32 *desc, u32 command) |
| 110 | { |
| 111 | u32 *cmd = desc_end(desc); |
| 112 | |
| 113 | *cmd = command; |
| 114 | |
| 115 | (*desc)++; |
| 116 | } |
| 117 | |
| 118 | #define append_u32 append_cmd |
| 119 | |
| 120 | static inline void append_u64(u32 *desc, u64 data) |
| 121 | { |
| 122 | u32 *offset = desc_end(desc); |
| 123 | |
| 124 | *offset = upper_32_bits(data); |
| 125 | *(++offset) = lower_32_bits(data); |
| 126 | |
| 127 | (*desc) += 2; |
| 128 | } |
| 129 | |
| 130 | /* Write command without affecting header, and return pointer to next word */ |
| 131 | static inline u32 *write_cmd(u32 *desc, u32 command) |
| 132 | { |
| 133 | *desc = command; |
| 134 | |
| 135 | return desc + 1; |
| 136 | } |
| 137 | |
| 138 | static inline void append_cmd_ptr(u32 *desc, dma_addr_t ptr, int len, |
| 139 | u32 command) |
| 140 | { |
| 141 | append_cmd(desc, command | len); |
| 142 | append_ptr(desc, ptr); |
| 143 | } |
| 144 | |
| 145 | /* Write length after pointer, rather than inside command */ |
| 146 | static inline void append_cmd_ptr_extlen(u32 *desc, dma_addr_t ptr, |
| 147 | unsigned int len, u32 command) |
| 148 | { |
| 149 | append_cmd(desc, command); |
| 150 | if (!(command & (SQIN_RTO | SQIN_PRE))) |
| 151 | append_ptr(desc, ptr); |
| 152 | append_cmd(desc, len); |
| 153 | } |
| 154 | |
| 155 | static inline void append_cmd_data(u32 *desc, void *data, int len, |
| 156 | u32 command) |
| 157 | { |
| 158 | append_cmd(desc, command | IMMEDIATE | len); |
| 159 | append_data(desc, data, len); |
| 160 | } |
| 161 | |
| 162 | #define APPEND_CMD_RET(cmd, op) \ |
| 163 | static inline u32 *append_##cmd(u32 *desc, u32 options) \ |
| 164 | { \ |
| 165 | u32 *cmd = desc_end(desc); \ |
| 166 | PRINT_POS; \ |
| 167 | append_cmd(desc, CMD_##op | options); \ |
| 168 | return cmd; \ |
| 169 | } |
| 170 | APPEND_CMD_RET(jump, JUMP) |
| 171 | APPEND_CMD_RET(move, MOVE) |
| 172 | |
| 173 | static inline void set_jump_tgt_here(u32 *desc, u32 *jump_cmd) |
| 174 | { |
| 175 | *jump_cmd = *jump_cmd | (desc_len(desc) - (jump_cmd - desc)); |
| 176 | } |
| 177 | |
| 178 | static inline void set_move_tgt_here(u32 *desc, u32 *move_cmd) |
| 179 | { |
| 180 | *move_cmd &= ~MOVE_OFFSET_MASK; |
| 181 | *move_cmd = *move_cmd | ((desc_len(desc) << (MOVE_OFFSET_SHIFT + 2)) & |
| 182 | MOVE_OFFSET_MASK); |
| 183 | } |
| 184 | |
| 185 | #define APPEND_CMD(cmd, op) \ |
| 186 | static inline void append_##cmd(u32 *desc, u32 options) \ |
| 187 | { \ |
| 188 | PRINT_POS; \ |
| 189 | append_cmd(desc, CMD_##op | options); \ |
| 190 | } |
| 191 | APPEND_CMD(operation, OPERATION) |
| 192 | |
| 193 | #define APPEND_CMD_LEN(cmd, op) \ |
| 194 | static inline void append_##cmd(u32 *desc, unsigned int len, u32 options) \ |
| 195 | { \ |
| 196 | PRINT_POS; \ |
| 197 | append_cmd(desc, CMD_##op | len | options); \ |
| 198 | } |
| 199 | APPEND_CMD_LEN(seq_store, SEQ_STORE) |
| 200 | APPEND_CMD_LEN(seq_fifo_load, SEQ_FIFO_LOAD) |
| 201 | APPEND_CMD_LEN(seq_fifo_store, SEQ_FIFO_STORE) |
| 202 | |
| 203 | #define APPEND_CMD_PTR(cmd, op) \ |
| 204 | static inline void append_##cmd(u32 *desc, dma_addr_t ptr, unsigned int len, \ |
| 205 | u32 options) \ |
| 206 | { \ |
| 207 | PRINT_POS; \ |
| 208 | append_cmd_ptr(desc, ptr, len, CMD_##op | options); \ |
| 209 | } |
| 210 | APPEND_CMD_PTR(key, KEY) |
| 211 | APPEND_CMD_PTR(load, LOAD) |
| 212 | APPEND_CMD_PTR(fifo_load, FIFO_LOAD) |
| 213 | APPEND_CMD_PTR(fifo_store, FIFO_STORE) |
| 214 | |
| 215 | static inline void append_store(u32 *desc, dma_addr_t ptr, unsigned int len, |
| 216 | u32 options) |
| 217 | { |
| 218 | u32 cmd_src; |
| 219 | |
| 220 | cmd_src = options & LDST_SRCDST_MASK; |
| 221 | |
| 222 | append_cmd(desc, CMD_STORE | options | len); |
| 223 | |
| 224 | /* The following options do not require pointer */ |
| 225 | if (!(cmd_src == LDST_SRCDST_WORD_DESCBUF_SHARED || |
| 226 | cmd_src == LDST_SRCDST_WORD_DESCBUF_JOB || |
| 227 | cmd_src == LDST_SRCDST_WORD_DESCBUF_JOB_WE || |
| 228 | cmd_src == LDST_SRCDST_WORD_DESCBUF_SHARED_WE)) |
| 229 | append_ptr(desc, ptr); |
| 230 | } |
| 231 | |
| 232 | #define APPEND_SEQ_PTR_INTLEN(cmd, op) \ |
| 233 | static inline void append_seq_##cmd##_ptr_intlen(u32 *desc, dma_addr_t ptr, \ |
| 234 | unsigned int len, \ |
| 235 | u32 options) \ |
| 236 | { \ |
| 237 | PRINT_POS; \ |
| 238 | if (options & (SQIN_RTO | SQIN_PRE)) \ |
| 239 | append_cmd(desc, CMD_SEQ_##op##_PTR | len | options); \ |
| 240 | else \ |
| 241 | append_cmd_ptr(desc, ptr, len, CMD_SEQ_##op##_PTR | options); \ |
| 242 | } |
| 243 | APPEND_SEQ_PTR_INTLEN(in, IN) |
| 244 | APPEND_SEQ_PTR_INTLEN(out, OUT) |
| 245 | |
| 246 | #define APPEND_CMD_PTR_TO_IMM(cmd, op) \ |
| 247 | static inline void append_##cmd##_as_imm(u32 *desc, void *data, \ |
| 248 | unsigned int len, u32 options) \ |
| 249 | { \ |
| 250 | PRINT_POS; \ |
| 251 | append_cmd_data(desc, data, len, CMD_##op | options); \ |
| 252 | } |
| 253 | APPEND_CMD_PTR_TO_IMM(load, LOAD); |
| 254 | APPEND_CMD_PTR_TO_IMM(fifo_load, FIFO_LOAD); |
| 255 | |
| 256 | #define APPEND_CMD_PTR_EXTLEN(cmd, op) \ |
| 257 | static inline void append_##cmd##_extlen(u32 *desc, dma_addr_t ptr, \ |
| 258 | unsigned int len, u32 options) \ |
| 259 | { \ |
| 260 | PRINT_POS; \ |
| 261 | append_cmd_ptr_extlen(desc, ptr, len, CMD_##op | SQIN_EXT | options); \ |
| 262 | } |
| 263 | APPEND_CMD_PTR_EXTLEN(seq_in_ptr, SEQ_IN_PTR) |
| 264 | APPEND_CMD_PTR_EXTLEN(seq_out_ptr, SEQ_OUT_PTR) |
| 265 | |
| 266 | /* |
| 267 | * Determine whether to store length internally or externally depending on |
| 268 | * the size of its type |
| 269 | */ |
| 270 | #define APPEND_CMD_PTR_LEN(cmd, op, type) \ |
| 271 | static inline void append_##cmd(u32 *desc, dma_addr_t ptr, \ |
| 272 | type len, u32 options) \ |
| 273 | { \ |
| 274 | PRINT_POS; \ |
| 275 | if (sizeof(type) > sizeof(u16)) \ |
| 276 | append_##cmd##_extlen(desc, ptr, len, options); \ |
| 277 | else \ |
| 278 | append_##cmd##_intlen(desc, ptr, len, options); \ |
| 279 | } |
| 280 | APPEND_CMD_PTR_LEN(seq_in_ptr, SEQ_IN_PTR, u32) |
| 281 | APPEND_CMD_PTR_LEN(seq_out_ptr, SEQ_OUT_PTR, u32) |
| 282 | |
| 283 | /* |
| 284 | * 2nd variant for commands whose specified immediate length differs |
| 285 | * from length of immediate data provided, e.g., split keys |
| 286 | */ |
| 287 | #define APPEND_CMD_PTR_TO_IMM2(cmd, op) \ |
| 288 | static inline void append_##cmd##_as_imm(u32 *desc, void *data, \ |
| 289 | unsigned int data_len, \ |
| 290 | unsigned int len, u32 options) \ |
| 291 | { \ |
| 292 | PRINT_POS; \ |
| 293 | append_cmd(desc, CMD_##op | IMMEDIATE | len | options); \ |
| 294 | append_data(desc, data, data_len); \ |
| 295 | } |
| 296 | APPEND_CMD_PTR_TO_IMM2(key, KEY); |
| 297 | |
| 298 | #define APPEND_CMD_RAW_IMM(cmd, op, type) \ |
| 299 | static inline void append_##cmd##_imm_##type(u32 *desc, type immediate, \ |
| 300 | u32 options) \ |
| 301 | { \ |
| 302 | PRINT_POS; \ |
| 303 | append_cmd(desc, CMD_##op | IMMEDIATE | options | sizeof(type)); \ |
| 304 | append_cmd(desc, immediate); \ |
| 305 | } |
| 306 | APPEND_CMD_RAW_IMM(load, LOAD, u32); |